回头一顾一相思,佳时堪恨再难寻

Springboot & Mybatis 构建restful 服务二

1 前置条件

  • 成功执行完Springboot & Mybatis 构建restful 服务一

2 restful service 打包问题

​ 要求打包成以下格式:

 
---tar
   
   ---jar      Service的源代码和 Pom.xml
   
   ---lib      Service的依赖jar包
   
   ---config   Service的配置文件
 

3 配置文件外置

1)修改 application.properties 文件

src/main/resources/application.properties

 
  #新增
  #配置Oracle数据库的驱动类
  spring.datasource.driverClass=oracle.jdbc.driver.OracleDriver
  #配置本地类路径的入口
  spring.classPathEntry.location=classpath:lib/ojdbc6-1.0.1.jar
 

2)修改generatorConfig.xml:

 
    <!-- 修改classPathEntry里 location和jdbcConnection的数据库连接,设置从资源文件中读取值 -->
    <classPathEntry  location="${spring.classPathEntry.location}" />  
    <jdbcConnection  driverClass="${spring.datasource.driverClass}"
                     connectionURL="${spring.datasource.url}"
                     userId="${spring.datasource.username}"                                                                   password="${spring.datasource.password}">
    </jdbcConnection>
 

3)修改 POM:

在build 里新增以下代码:用于打包时只打包后缀名为 .java的文件,忽略配置文件(src/main/resources)。并将该目录下所有的文件复制到${project.build.directory}/config

 
    <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.*</include>
                </includes>
                <filtering>true</filtering>
                <targetPath>${project.build.directory}/config</targetPath>
            </resource>
            <resource> 
                <directory>src/main/java</directory> 
                <excludes> 
                    <exclude>**/*.java</exclude> 
                </excludes> 
            </resource>  
        </resources>
 

添加maven-jar-plugin插件,指定主类和依赖 jar 所在位置

(此例中为:main/java/com/serena)

  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-jar-plugin</artifactId>  
                <version>2.4</version>  
                <configuration>  
                    <archive>  
                        <manifest>  
                            <addClasspath>true</addClasspath>  
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.serena.App</mainClass>  
                        </manifest>  
                     </archive>  
                </configuration> 
             </plugin>  
 

添加maven-dependency-plugin插件,将所有依赖 jar 复制到${project.build.directory}/lib目录

 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin> 
 

添加maven-antrun-plugin插件,用于将生成的可运行的 jar ,配置文件,依赖 jar,启动脚本等打包成tar压缩包

 
    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <tasks>
                        <mkdir dir="${project.build.directory}/config" />
                        <mkdir dir="${project.build.directory}/lib" />
                        <mkdir dir="${project.build.directory}/bin" />
                        <mkdir dir="${project.build.directory}/log" />
                        <tar tarfile="${project.build.directory}/${project.build.finalName}.tar"
                                    basedir="${project.build.directory}"                                includes="*.xml,config/*,log,bin,bin/*,lib/*,startup.sh,shutdown.sh,${project.build.finalName}.jar" />
                    </tasks>
                </configuration>
            </execution>
         </executions>
    </plugin>
 

4.在终端输入如下测试指令

 
#cd 项目所在目录
cd /Users/psj/Documents/pro/xm/AccountBalance
mvn clean package
cd target
#将 tar 包复制到自己指定目录(/Users/psj/Desktop/t/)
cp AccountBalance-0.0.1-SNAPSHOT.tar /Users/psj/Desktop/t/
#cd 到上个操作指定的目录
cd /Users/psj/Desktop/t
#解压 tar 包
tar -xvf AccountBalance-0.0.1-SNAPSHOT.tar
#此时可查看目录结构如要求所示
ll
#运行 可执行jar,测试结果
java -jar AccountBalance-0.0.1-SNAPSHOT.jar
#
#打开新的iterm 窗口(command+n)
http localhost:8101/accounts
http localhost:8101/account/U00001
#
#返回上个 iterm 窗口,control+c 结束服务。
 

 

 

 

posted on 2017-03-07 16:22  serena_uuu  阅读(396)  评论(0编辑  收藏  举报