maven打包web项目时同时打包为war和jar文件

时间:2021-09-26 09:36:33

首先在pom.xml文件中指定war的打包方式,war

    <artifactId>test</artifactId>
<name>test</name>
<packaging>war</packaging>

上述代码在eclipse中执行maven install时, 会默认打成war,并放入本地仓库。

web项目时同时打包为war和jar文件
1、首先添加在pom.xml中添加插件 maven-jar-plugin , 使得 在调用命令mvn package install 或者 mvn package deploy 先生成 jar包。或者使用mvn package生成jar包。该插件生成的架包会放在工程的target文件夹下。

2、然后配置maven-install-plugin 插件, 使得在eclipse中执行maven install时, 同时生成jar和war到本地仓库。

3、再配置org.apache.maven.plugins插件, 使得在eclipse中执行deploy 时, 同时,生成jar和war到远程仓库。

源码如下:

<!-- package jar on package -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- install jar to local repository -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>
<!-- deploy jar to remote repository -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<generatePom>true</generatePom>
<url>${project.distributionManagement.repository.url}</url>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>${project.build.directory}/${project.artifactId}.jar</file>
</configuration>
</execution>
</executions>
</plugin>