maven配置同时打包war和jar包

maven | 2019-09-13 10:02:39

当你的项目是war项目而且你的项目又有些类提供给其他项目使用,那么你就需要:maven配置同时打包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到远程仓库。


pom.xml配置

<artifactId>itxwServer</artifactId>
<packaging>war</packaging>

<build>
    <plugins>
      <!-- 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>
                             <!--<file>${project.build.directory}/${project.artifactId}.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>
    </plugins>
    <finalName>itxwServer</finalName>
</build>



登录后即可回复 登录 | 注册
    
关注编程学问公众号