简体   繁体   English

是否可以重命名maven jar-with-dependencies?

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

I'm currently using the jar-with-dependencies assembly to create such a jar. 我目前正在使用jar-with-dependencies程序集来创建这样一个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. 由于这个jar正被AS400上的RPG程序使用,我想缩短它以使这些开发人员的生活更轻松。 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 . 但是,除了手工之外,我还没有找到一种方法来从通常的project-name-version-classifier-jar-with-dependencies.jar重命名jar。 I'd like something like project-name-version-classifier-full.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. 我的分类器的jar包含构建所在的区域。 The jar with all dependencies which also includes the region. 具有所有依赖关系的jar也包括该区域。

project-name-version-region-full.jar and project-name-version-region.jar should be stored in the repository. project-name-version-region-full.jarproject-name-version-region.jar应存储在存储库中。 In the first example the classifier is region-full, in the 2nd it's region. 在第一个例子中,分类器是区域满的,在第二个区域。 The latter is working. 后者正在发挥作用。

You can specify the finalName property to give the jar the name you want, and specify that appendAssemblyId should 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. 我相信这是最近版本的assembly-plugin中的一个错误 - 他们已经删除了对分类器的支持,但是如果你使用内置的描述符,id就会被修复,所以你最终会得到一个很大的愚蠢名称。

As a workaround, you can copy the assembly descriptor used by the jar-with-dependencies descriptor 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 region and the assembly id as full . 此示例将导致将程序集ID附加到finalName,因此如果您需要具有region-full.jar的名称,则可以将finalName指定为region ,将程序集ID指定为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 full used as the classifier. 这将导致目标中的文件名为region-full.jar,但请注意,它仍将作为附加工件安装到Maven存储库,并完全用作分类器。 As long as this id is different to that for your other assembly there should be no collision though. 只要这个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>

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. 除了使用artifactId和version指定finalName之外,它与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>

Thanks to the posts here and some digging in the maven docs I'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.jar with all of its dependencies re-packaged into that same jar and the specified Main-Class: karlthepagain.MyMain . 这将生成MyJarName.jar ,其所有依赖项重新打包到同一个jar和指定的Main-Class: karlthepagain.MyMain

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. 想象一下,当我了解到程序集xml可以通过构建中的项进行参数化时,我会感到惊讶。 This was exactly what I was looking for. 这正是我想要的。

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>

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM