使用maven插件构建项目安装包

纵然是瞬间 提交于 2020-03-02 18:23:39

    因项目需要,需要在一开源项目上做二 次开发,对开发后的项目需要再构建出自己的安装包(如Linux下的*.tar.gz,Windows下的*.zip格式)。从网上搜索资料发现maven插件可以实现这个需求。现把已经验证成功的maven的插件配置pom文件分享。以备有共同需要的人可以参考,同时给自己做个备忘:

pom.xml内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>xxxx</groupId>
  <artifactId>xxxxx</artifactId>
  <version>3.4.6</version>
  <name>xxxx</name>
  <description>xxxxx</description>
  
  <properties>
    <target.dir>target</target.dir>
  </properties>

  <dependencies>
    <dependency>
      ....
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- 指定多个源代码目录、多个资源文件目录 -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/java/main</source>
                <source>src/java/generated</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- 编译插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
           <encoding>UTF8</encoding> 
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <!-- 生成源文件jar包文件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1.1</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- 打字节码包插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <includes>
            <classesDirectory>org/</classesDirectory>
          </includes>
          <executions>
            <execution>
              <id>make-a-jar</id>
              <phase>compile</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </configuration>
      </plugin>
      <!-- 部署插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptors>
            <descriptor>assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

其中,assembly.xml是部署插件maven-assembly-plugin的配置文件,在assembly.xml里指定哪些资源需要部署到安装包中。其内容如下:

<assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-2.2.xsd">
  <id>full</id>
  <formats>
    <format>tar.gz</format> <!--Linux系统下打包文件格式 -->
    <format>zip</format> <!--Windows系统下打包文件格式 -->
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory><!-- 将scope为runtime的依赖包打包到lib目录下。 -->
      <scope>runtime</scope>
      <useProjectArtifact>false</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>/</outputDirectory>
      <directory>/</directory>
      <includes>
        <include>bin/*</include>
        <include>conf/*</include>
        <include>*.xml</include>
        <include>*.txt</include>
      </includes>
      <excludes>
        <exclude>pom.xml</exclude>
        <exclude>build.xml</exclude>
        <exclude>build_bak.xml</exclude>
        <exclude>assembly.xml</exclude>
      </excludes>
    </fileSet>
     <fileSet>  
            <directory>/target/</directory>  
            <outputDirectory>/</outputDirectory>  
            <includes>
              <include>*.jar</include>
            </includes>
        </fileSet> 
  </fileSets>
</assembly>


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!