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

后端 未结 6 665
一整个雨季
一整个雨季 2021-01-29 19:47

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

相关标签:
6条回答
  • 2021-01-29 20:25

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

    <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>
    
    0 讨论(0)
  • 2021-01-29 20:33

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

    It's basically the same as Rich's answer, except the finalName is specified with the artifactId and 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>
    
    0 讨论(0)
  • 2021-01-29 20:36

    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.

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

    0 讨论(0)
  • 2021-01-29 20:43

    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.

    The configuration below will output a jar called "test.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.

    As a workaround, you can copy the assembly descriptor used by the jar-with-dependencies descriptor and modify the 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. 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. As long as this id is different to that for your other assembly there should be no collision though.

    The pom configuration would look like this.

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

    <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>
    
    0 讨论(0)
  • 2021-01-29 20:44

    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>
    
    0 讨论(0)
  • 2021-01-29 20:45

    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:

    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:

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

    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.

    0 讨论(0)
提交回复
热议问题