随着 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.ttit.provider.Main</mainClass> </transformer> </transformers> </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>