Maven - 用于压缩jar文件的简单插件

时间:2016-09-06 06:08:22

标签: java maven

模块项目。

 -1
  -pom.xml
 -2
  -pom.xml
 -3 
  -pom.xml
 -parent
  -pom.xml

并且所有模块都由父pom继承。我所做的是编辑父pom,我想创建一个插件来构建具有2个罐子的拉链(从-1和-2)

我尝试了类似的东西

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution> <id>createDistJar</id> 
                    <goals> <goal>run</goal> </goals>  <phase>package</phase>
                    <configuration>
                        <target>
                            <echo message="${project.build.directory}"/>
                            <mkdir dir="${project.build.directory}"/>
                            <zip destfile="${project.build.directory}/JawaBot-${project.version}-dist.zip"
                                basedir="target/" includes="JawaBot-${project.version}-dist-rh/**">
                            </zip>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>uploadDistJar</id> <goals>  <goal>attach-artifact</goal>  </goals>
                    <phase>package</phase>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>${project.build.directory}/JawaBot-${project.version}-dist.zip</file>
                                <type>zip</type>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>

但它会为所有/ targer文件夹中的所有模块创建压缩文件。

1 个答案:

答案 0 :(得分:1)

使用补充pom文件创建一个单独的模块(xyz-dist),该文件包含您希望参与分发的模块的依赖项。

在父pom中添加dist模块,如下所示:

  <modules>
      <module>dist</module>
      <module>package-1</module>
      <module>package-2</module>
  </modules>

dist模块中,将以下内容添加到您的pom中:

<project...>

  <parent>
  </parent>

  <packaging>pom</packaging>

  <dependencies>
      Dependencies of the modules you would like to package
  </dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>make-bundles</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/assembly/dist.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

dist.xml应包含以下内容:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

  <id>dist</id>

  <formats>
      <format>zip</format>
  </formats>

  <includeBaseDirectory>false</includeBaseDirectory>

  <dependencySets>
      <dependencySet>
          <outputDirectory>/</outputDirectory>
          <useProjectArtifact>false</useProjectArtifact>
          <unpack>false</unpack>
          <scope>runtime</scope>
      </dependencySet>
  </dependencySets>
</assembly>