零、跳过单元测试
<!-- 跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<skip>true</skip>
<!--<source>${java.version}</source>
<target>${java.version}</target>-->
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
一、打包
1、在pom.xml中指定main类
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 此处指定main方法入口的class -->
<mainClass>com.a.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
普通项目达成一个jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
2、打第三方包的mvn命令
#dependency:copy-dependencies 是将依赖的包打到dependency目录下
打包:clean package dependency:copy-dependencies
#-DoutputDirectory=lib 是将依赖的包打到指定(lib)目录下
打包:clean package dependency:copy-dependencies -DoutputDirectory=lib
二、项目启动
例:
jar包path: D:\software\eclispe-workspace\eclipse13\sunxj-package\target\lib\sunxj-package-0.0.1-SNAPSHOT.jar
D:\software\eclispe-workspace\eclipse13\sunxj-package\target>java -jar lib/sunxj-package-0.0.1-SNAPSHOT.jar com.a
+++++++++++++package-test++++++++++++++++
D:\software\eclispe-workspace\eclipse13\sunxj-package\target>java -classpath lib/* com.a.App
+++++++++++++package-test++++++++++++++++
D:\software\eclispe-workspace\eclipse13\sunxj-package\target>java -cp lib/* com.a.App
+++++++++++++package-test++++++++++++++++ 注:类加载器为app
D:\software\eclispe-workspace\eclipse13\sunxj-package\target>java -Djava.ext.dirs=lib com.a.App
+++++++++++++package-test++++++++++++++++ 注:类加载器为ext
三、第三方包打到lib下
1、pom配置:
<build>
<!-- 用指定的jdk版本打jar包 -->
<plugins>
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> -->
<!-- 将依赖的jar包打到lib目录下 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- 指定出程序的入口类 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- 依赖JAR包输出到了lib -->
<mainClass>com.test.service.ServerStartup</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、目录结构
3、启动脚本
java -jar sunxj-mail-0.0.1-SNAPSHOT.jar
四、SpringBoot--使用appassembler-maven-plugin打包
appassembler-maven-plugin是一个用来为Java应用打包并生成启动脚本的Maven插件,使用起来非常简单,只需要在项目的pom.xml中加入插件的相关配置即可。它在脚本打包过程中,能够将项目的所有依赖自动复制到指定的打包目录中,并将这些依赖添加到classpath中。
官网地址:http://www.mojohaus.org/appassembler/appassembler-maven-plugin/
一:生成可执行的启动脚本
打包命令:mvn clean package appassembler:assemble
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<!-- 生成linux, windows两种平台的执行脚本 -->
<platforms>
<platform>unix</platform>
<platform>windows</platform>
</platforms>
<!-- 包存放的根目录 -->
<assembleDirectory>${project.build.directory}/${project.name}</assembleDirectory>
<!-- 打包的jar,以及maven依赖的jar存放目录 -->
<repositoryName>lib</repositoryName>
<!-- lib目录中jar的存放规则,默认是${groupId}/${artifactId}的目录格式,flat表示直接把jar放到lib目录 -->
<!-- 可执行脚本的存放目录 -->
<binFolder>bin</binFolder>
<!-- 配置文件的存放目录 -->
<configurationDirectory>conf</configurationDirectory>
<!-- 拷贝配置文件到上面的目录中 -->
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<!-- 从哪里拷贝配置文件 (默认src/main/config) -->
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<repositoryLayout>flat</repositoryLayout>
<encoding>UTF-8</encoding>
<logsDirectory>logs</logsDirectory>
<tempDirectory>tmp</tempDirectory>
<programs>
<program>
<!-- 启动类 -->
<mainClass>com.pengjunlee.MyApplication</mainClass>
<jvmSettings>
<extraArguments>
<extraArgument>-server</extraArgument>
<extraArgument>-Xmx1G</extraArgument>
<extraArgument>-Xms1G</extraArgument>
</extraArguments>
</jvmSettings>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
二:生成后台服务程序
打包命令:mvn clean package appassembler:generate-daemons
Usage: { console | start | stop | restart | status | dump }
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<!-- 根目录 -->
<assembleDirectory>${project.build.directory}/${project.name}</assembleDirectory>
<!-- 打包的jar,以及maven依赖的jar存放目录 -->
<repositoryName>lib</repositoryName>
<!-- 可执行脚本的存放目录 -->
<binFolder>bin</binFolder>
<!-- 配置文件的存放目录 -->
<configurationDirectory>conf</configurationDirectory>
<!-- 拷贝配置文件到上面的目录中 -->
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<!-- 从哪里拷贝配置文件 (默认src/main/config) -->
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<!-- lib目录中jar的存放规则,默认是${groupId}/${artifactId}的目录格式,flat表示直接把jar放到lib目录 -->
<repositoryLayout>flat</repositoryLayout>
<encoding>UTF-8</encoding>
<logsDirectory>logs</logsDirectory>
<tempDirectory>tmp</tempDirectory>
<daemons>
<daemon>
<mainClass>com.pengjunlee.MyApplication</mainClass>
<platforms>
<platform>jsw</platform>
</platforms>
<generatorConfigurations>
<generatorConfiguration>
<generator>jsw</generator>
<includes>
<include>linux-x86-32</include>
<include>linux-x86-64</include>
<include>windows-x86-32</include>
<include>windows-x86-64</include>
</includes>
<configuration>
<property>
<name>configuration.directory.in.classpath.first</name>
<value>conf</value>
</property>
<property>
<name>wrapper.ping.timeout</name>
<value>120</value>
</property>
<property>
<name>set.default.REPO_DIR</name>
<value>lib</value>
</property>
<property>
<name>wrapper.logfile</name>
<value>logs/wrapper.log</value>
</property>
</configuration>
</generatorConfiguration>
</generatorConfigurations>
<jvmSettings>
<!-- jvm参数 -->
<systemProperties>
<systemProperty>com.sun.management.jmxremote</systemProperty>
<systemProperty>com.sun.management.jmxremote.port=1984</systemProperty>
<systemProperty>com.sun.management.jmxremote.authenticate=false</systemProperty>
<systemProperty>com.sun.management.jmxremote.ssl=false</systemProperty>
</systemProperties>
</jvmSettings>
</daemon>
</daemons>
</configuration>
</plugin>
</plugins>
</build>
五、SpringBoot--使用appassembler-maven-plugin打包
1、pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>a.App</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>a.App</mainClass> <!-- 你的主类名 -->
</manifest>
</archive>
<finalName>sunxj-package</finalName>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
注:spring-boot-maven-plugin可有可无。有时,启动脚本为java -jar sunxj-package.jar。没有时,启动脚本为java -cp ./lib/* a.App
2、assembly.xml
<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>${project.version}</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>bin</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>provided</scope>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>system</scope>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
六、springboot项目打成一个jar包
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>com.App</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
启动:java -jar sunxj-deploy.jar
jvm启动脚本可以用“-cp”指定多个文件夹、文件、jar包,用“:”分割:
/export/servers/jdk1.8.0_20/jre/bin/java -Xms5120m -Xmx5120m -XX:MetaspaceSize=128M -XX:MaxDirectMemorySize=96m -Dio.netty.allocator.type=pooled -Dio.netty.allocator.tinyCacheSize=0 -Dio.netty.allocator.smallCacheSize=0 -Dio.netty.allocator.normalCacheSize=0 -Dagent.uuid=5ad1973f51f64e8c9498cc3410e4e334 -Dtest.id=282405 -Dtestcase.id=390637 -cp /export/forcebot_bigfile/390637:/export/forcebot_cfs_local/390637/attachment:/export/forcebot_cfs_local/390637/attachment/1.txt:/export/forcebot_cfs_local/390637/attachment/pin-coupon.txt:/export/forcebot_cfs_local/390637/attachment/sunxj-genfile-1.0-SNAPSHOT.jar:/export/App/conf/:/export/App/lib/aws-java-sdk-core-1.11.600.jar:/export/App/lib/aws-java-sdk-kms-1.11.600.jar:/export/App/lib/aws-java-sdk-s3-1.11.600.jar:/export/App/lib/commons-beanutils-1.9.3.jar:/export/App/lib/commons-codec-1.11.jar:/export/App/lib/commons-collections-3.2.2.jar:/export/App/lib/commons-compiler-3.0.7.jar:/export/App/lib/commons-lang3-3.10.jar:/export/App/lib/commons-logging-1.2.jar:/export/App/lib/fastjson-1.2.72.jar:/export/App/lib/forcebot-agent-worker-3.0.0-SNAPSHOT.jar:/export/App/lib/forcebot-engine-3.0.0-SNAPSHOT.jar:/export/App/lib/forcebot-netty-3.0.0-SNAPSHOT.jar:/export/App/lib/forcebot-toolkit-3.0.0-SNAPSHOT.jar:/export/App/lib/groovy-all-2.4.11.jar:/export/App/lib/hamcrest-core-1.3.jar:/export/App/lib/httpclient-4.5.12.jar:/export/App/lib/httpcore-4.4.13.jar:/export/App/lib/ion-java-1.0.2.jar:/export/App/lib/jackson-annotations-2.8.0.jar:/export/App/lib/jackson-core-2.8.9.jar:/export/App/lib/jackson-core-asl-1.9.2.jar:/export/App/lib/jackson-databind-2.8.9.jar:/export/App/lib/jackson-dataformat-cbor-2.6.2.jar:/export/App/lib/jackson-mapper-asl-1.9.2.jar:/export/App/lib/janino-3.0.7.jar:/export/App/lib/javassist-3.18.1-GA.jar:/export/App/lib/jcommander-1.72.jar:/export/App/lib/jmespath-java-1.11.600.jar:/export/App/lib/joda-time-2.8.2.jar:/export/App/lib/json-simple-1.1.1.jar:/export/App/lib/junit-4.12.jar:/export/App/lib/logback-classic-1.2.3.jar:/export/App/lib/logback-core-1.2.3.jar:/export/App/lib/lombok-1.18.4.jar:/export/App/lib/msgpack-0.6.12.jar:/export/App/lib/netty-all-4.1.8.Final.jar:/export/App/lib/okhttp-3.6.0.jar:/export/App/lib/okio-1.11.0.jar:/export/App/lib/open-api-sdk-2.0.jar:/export/App/lib/opencsv-3.9.jar:/export/App/lib/slf4j-api-1.7.30.jar com.jd.forcebot.worker.process.WorkerEntryPoint -worker-id 0 -console-port 35501 -log-level ERROR -log-output FILE -log-path /export/Logs/forcebot-agent/forcebot-agent-worker-0.log -log-max-file-size 512 -log-total-size 2048
七、将项目所依赖的jar文件名称输出到指定文件
说明:在generate-sources阶段执行build-classpath
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>sunxj-dependencies.txt</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>dependency-scan</id>
<phase>generate-sources</phase>
<configuration>
<fileSeparator>/</fileSeparator>
<pathSeparator>;</pathSeparator>
<includeScope>runtime</includeScope>
<outputFile>${project.basedir}/target/classes/sunxj-dependencies.txt
</outputFile>
</configuration>
<goals>
<goal>build-classpath</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>