154

I'm currently using the jar-with-dependencies assembly to create such a jar. However, the name of my jar is a bit long.

Since this jar is being used by RPG programs on an AS400, I'd like to shorten it to make life a bit easier for those developers. But, other than by hand, I've not found a way to rename the jar from the usual project-name-version-classifier-jar-with-dependencies.jar. I'd like something like project-name-version-classifier-full.jar

Is there anyway to do this without basically copying the jar-with-dependencies assembly descriptor and calling it full?

Additionally, I want to continue to have the jar without the classpath assembled stored in the repository.

I need two artifacts. The jar with my classifier holding the region which the build is for. The jar with all dependencies which also includes the region.

project-name-version-region-full.jar and project-name-version-region.jar should be stored in the repository. In the first example the classifier is region-full, in the 2nd it's region. The latter is working.

4

6 回答 6

260

您可以指定finalName属性为 jar 指定您想要的名称,并指定appendAssemblyId应为 false 以避免“jar-with-dependencies”后缀。

下面的配置将输出一个名为“test.jar”的jar

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-4</version>
  <executions>
    <execution>
      <id>jar-with-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <finalName>test</finalName>
        <appendAssemblyId>false</appendAssemblyId>
      </configuration>
    </execution>
  </executions>
</plugin>

更新:根据您的评论,使用内置描述符将不起作用。我相信这归结为最新版本的程序集插件中的一个错误 - 他们已经删除了对分类器的支持,但是如果你使用内置描述符,id 是固定的,所以你最终会得到一个大傻瓜的名字。

作为一种解决方法,您可以复制jar-with-dependencies描述符使用的程序集描述符并修改 id。

此示例将导致程序集 id 附加到 finalName,因此如果您需要具有region-full.jar的名称,则可以将 finalName 指定为region并将程序集 id 指定为full。这将在目标中生成一个名为 region-full.jar 的文件,但请注意,它仍将作为附件安装到 Maven 存储库中,并使用full作为分类器。只要此 ID 与您的其他程序集的 ID 不同,就应该不会发生冲突。

pom 配置看起来像这样。

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-4</version>
  <executions>
    <execution>
      <id>jar-with-dependencies</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>src/main/assembly/jar-assembly.xml</descriptor>
        </descriptors>
        <finalName>region</finalName>
      </configuration>
    </execution>
  </executions>
</plugin>

和 src/main/assembly 中的 jar-assembly.xml 如下所示:

<assembly>
  <id>full</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
    </fileSet>
  </fileSets>
</assembly>
于 2009-08-26T14:28:05.763 回答
40

我想我已经找到了一种直接在 pom 中配置它而不需要单独的 jar-assembly.xml 的方法。

它与 Rich 的答案基本相同,除了 finalName 是用 artifactId 和 version 指定的。

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <finalName>${project.artifactId}-${project.version}-full</finalName>
        <appendAssemblyId>false</appendAssemblyId>
        <archive>
            <manifest>
                <mainClass>com.mycompany.MyMainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-my-jar-with-dependenciess</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
于 2013-01-19T18:19:52.807 回答
8

感谢这里的帖子和对maven 文档的一些挖掘,我为具有自定义名称的通用一次性重新打包的可执行 jar 程序集提出了以下配置。

在 pom.xml 中:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>exe</id>
            <phase>package</phase>
            <goals><goal>single</goal></goals>
            <configuration>
                <finalName>MyJarName</finalName>
                <attach>false</attach>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>karlthepagain.MyMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </execution>
    </executions>
</plugin>

在 assembly.xml 中:

<assembly>
    <id>exe</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

这将产生MyJarName.jar其所有依赖项重新打包到同一个 jar 和指定的Main-Class: karlthepagain.MyMain.

于 2011-03-08T06:10:49.337 回答
5

也可以使用${project.build.finalName}作为最终名称来覆盖原始 jar 文件:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
   <executions>
      <execution>
          <phase>package</phase>
          <goals>
             <goal>single</goal>
          </goals>
      </execution>
   </executions>
   <configuration>
     <descriptorRefs>
       <descriptorRef>jar-with-dependencies</descriptorRef>
     </descriptorRefs>
     <finalName>${project.build.finalName}</finalName>
     <appendAssemblyId>false</appendAssemblyId>
   </configuration>
 </plugin>
于 2019-07-01T10:49:51.410 回答
3

我要感谢 Rich 为我指明了正确的方向,但我想发布对我有用的解决方案,因为 Rich 的做法有点不对劲:

我的 jar-assembly.xml 看起来像这样,它允许为在我的配置文件中存储为属性的区域更改程序集 ID:

<assembly>
  <id>${env}-full</id>
    <formats>
      <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
      <dependencySet>
        <unpack>true</unpack>
        <scope>runtime</scope>
      </dependencySet>
    </dependencySets>
    <fileSets>
      <fileSet>
        <directory>${project.build.outputDirectory}</directory>
      </fileSet>
    </fileSets>
</assembly>

我没有在 maven-assembly-plugin 设置中使用 finalName 参数,因为它使用我的 project-name-version-env-full.jar 名称构建了我的项目,其中 env-full 是分类器。

想象一下,当我了解到程序集 xml 可以由构建中的项目参数化时,我会感到惊讶。这正是我想要的。

于 2009-08-26T18:05:11.113 回答
2

这对我有用

<build>
    <finalName>anynameyoulike</finalName>
    <plugins>           
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>                   
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                        <mainClass>com.mycompany.MyMainClass</mainClass>
                    </manifest>
                </archive>
            </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>
于 2016-07-21T19:14:44.737 回答