Java 是否可以重命名带有依赖项的 Maven jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1334999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:23:01  来源:igfitidea点击:

Is it possible to rename a maven jar-with-dependencies?

javamaven-2jarmaven-assembly-plugin

提问by Mike Cornell

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

我目前正在使用 jar-with-dependencies 程序集来创建这样的 jar。但是,我的罐子的名字有点长。

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

由于这个 jar 被 AS400 上的 RPG 程序使用,我想缩短它以使这些开发人员的生活更轻松。但是,除了手动,我还没有找到将 jar 从通常的project-name-version-classifier-jar-with-dependencies.jar. 我想要类似的东西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?

有没有办法做到这一点,而无需基本上复制 jar-with-dependencies 程序集描述符并将其完整调用?

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

此外,我想继续将没有组装类路径的 jar 存储在存储库中。

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.

我需要两个神器。带有我的分类器的 jar 包含构建所针对的区域。包含所有依赖项的 jar,其中还包括区域。

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

project-name-version-region-full.jar并且project-name-version-region.jar应该存储在存储库中。在第一个示例中,分类器是区域完整的,在第二个示例中是区域。后者正在工作。

采纳答案by Rich Seller

You can specify the finalNameproperty to give the jar the name you want, and specify that appendAssemblyIdshould be false to avoid the "jar-with-dependencies" suffix.

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

The configuration below will output a jar called "test.jar"

下面的配置将输出一个名为“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>


Update: based on your comments, using the built-in descriptor won't work . I believe this is down to a bug in the recent versions of the assembly-plugin - they've removed support for classifiers, but the id is fixed if you use a built-in descriptor, so you end up with a big daft name.

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

As a workaround, you can copy the assembly descriptor used by the jar-with-dependenciesdescriptor and modify the id.

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

This example would result in the assembly id being appended to the finalName, so if you need to have a name of region-full.jar, you can specify the finalName as regionand the assembly id as full. This will result in a file in target called region-full.jar, but note it will still be installed to the Maven repository as an attached artifact with fullused as the classifier. As long as this id is different to that for your other assembly there should be no collision though.

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

The pom configuration would look like this.

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>

and the jar-assembly.xml in src/main/assembly like this:

和 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>

回答by Mike Cornell

I'm going to give Rich the credit for pointing me in the right direction, but wanted to post the solution that worked for me as Rich's was slightly off:

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

My jar-assembly.xml looked like this which allowed the assembly id to change for the region which was stored as an property in my profile:

我的 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>

I did not use the finalName parameter in the maven-assembly-plugin settings as this built my project with my project-name-version-env-full.jar name where env-full was the classifier.

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

Imagine my surprise when I learned the assembly xml could be parameterized by items in the build. This was exactly what I was looking for.

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

回答by Karl the Pagan

Thanks to the posts here and some digging in the maven docsI've come up with the following configuration for a general one-off repacked executable jar assembly with a custom name.

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

In pom.xml:

在 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>

In assembly.xml:

在 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>

This will produce MyJarName.jarwith all of its dependencies re-packaged into that same jar and the specified Main-Class: karlthepagain.MyMain.

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

回答by vikingsteve

I think I've found a way to configure this directly in the pom without needing a separate jar-assembly.xml.

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

It's basically the same as Rich's answer, except the finalName is specified with the artifactId and version.

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

<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>

回答by umesh9883

This worked for me

这对我有用

<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>

回答by Mark Sch?fer

It is also possible to overwrite the original jar file by using ${project.build.finalName}as final name:

也可以使用${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>