隨着 Maven 使用的越來越多,我們希望Maven 將java項目打包。下面介紹幾種方式:
第一種,在pom.xml 中添加打包插件。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xattit.provider.RunMain</mainClass>
</transformer>
<!-- 解決spring 項目的xml 文件找不到問題 -->
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
<filters> <!-- 排除掉一些無用的文件 -->
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
注:打包插件其實就是個jar 包。具體版本可以到Maven repository 查找。兩位爲了生成可執行文件,需要證明程序運行的入口--mainClass。
我是在項目工作空間下運行mvn package 命令【清空命令 mvn clean】,截圖:
運行結果截圖如下:
在項目工作空間target 目錄下生成有兩個jar包。其中xxx-SNAPSHOT.jar 就是包含項目依賴包的可執行文件。另外一個不是。
在DOS下運行 java -jar xxx.jar,即可執行。
以下兩種生成jar 包的方式與第一種相同。
方式2:在pom.xml 中添加打包插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>com.ttit.provider.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
就先介紹着兩種生成jar 包的方式。這兩種方式參照微博:轉載原文
======================================================================
對於java web 項目可以生成可執行過war包,即在DOS下運行java -jar xx.war 即可執行web項目。
【這裏有個前提,項目中嵌入jetty自啓動類,也就是寫好程序jetty的啓動入口。用jetty做容器加載web項目】
原理與上述相似,在pom.xml 中添加生成war 的插件:
<build>
<finalName>TestWeb</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<version>3.0</version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- 引入打包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>main-class-placement</id>
<phase>prepare-package</phase>
<configuration>
<tasks>
<copy todir="${project.build.directory}/${project.artifactId}/">
<fileset dir="${project.build.directory}/classes/">
<include name="**/*/Main2.class" />
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.phda.test.Main2</mainClass>
</transformer>
</transformers>
<artifactSet>
<includes>
<include>org.eclipse.jetty:*</include>
<include>*:javax.servlet*</include>
<include>org.glassfish:javax.el*</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors> <!--描述文件路徑 -->
<descriptor>src/main/resources/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
有幾點需要說明:
1.網上有許多maven web項目生成可執行war 包的方法。前提是在項目添加web 自啓動類,多數估計使用的jetty。
2.上面貼出的是整個build 。這種方式中需要用到配置文件assembly.xml 代碼在下面;需要執行可執行類:mainClass;標紅的地方要根據實際項目情況而定。
3.我仍然實在項目工作空間下運行mvn package 獲得war 包。war 包生成在target 目錄下。
assembly.xml 內容如下:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>standalone</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
</dependencySets>
<files>
<file>
<source>target/${project.artifactId}.war</source>
<outputDirectory>\</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
<directory>src\main\assembly\bin</directory>
<outputDirectory>\bin</outputDirectory>
<includes>
<include>start.sh</include>
</includes>
<fileMode>0755</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
<fileSet>
<directory>src\main\assembly\bin</directory>
<outputDirectory>\bin</outputDirectory>
<includes>
<include>start.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>