How to use real jar names in manifest classpath us

2019-06-03 18:10发布

问题:

For some reason my client needs my artifacts without version in their names (MyArtifact.jar instead of MyArtifact-1.23.345.jar)

Therefor I added this configuration to my parent pom:

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                    <configuration>
                        <finalName>${project.artifactId}</finalName>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

This works as expected, meaning that I get jars of the child projects without versions generated in target folder.


However.

One of my jars is an executable jar which depends on the others. Currently I have the maven-jar-plugin configured for that subproject:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>build-classpath</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <fileSeparator>/</fileSeparator>
                <pathSeparator>;</pathSeparator>
                <outputProperty>bundle.classPath</outputProperty>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifestEntries>
                        <Class-Path>${bundle.classPath}</Class-Path>
                    </manifestEntries>
                </archive>
                <finalName>${project.artifactId}</finalName>
            </configuration>
        </plugin>
    </plugins>
</build>

The problem is that this generated classpath contains absolute paths to the artifacts on my PC.

Therefore I added the <prefix> tag to the configuration:

            <configuration>
                <prefix>lib</prefix>
                <fileSeparator>/</fileSeparator>
                <pathSeparator>;</pathSeparator>
                <outputProperty>bundle.classPath</outputProperty>
            </configuration>

But then the generated classpath includes the version numbers of the jars.

How can I omit the version numbers and the absolute paths in the classpath?

Problem is: I only want to remove Version numbers from my own artifacts, not from third party libs.

回答1:

To remove the version from copied dependencies, you can use the stripVersion option of the maven-dependency-plugin.

  1. In the aggregator pom use the dependency:copy-dependencies to copy your jars to some intermediate location.
  2. For you internal dependencies use <stripVersion>true</stripVersion>.
  3. For you 3rd party libraries use <stripVersion>false</stripVersion>.
  4. You may in-/exclude artifacts based on the group id.

For more detail you may look here.

EDIT:

This is to explain how the finalname works.

finalName: This is the name of the bundled project when it is finally built 
(sans the file extension, for example: my-project-1.0.jar). It defaults to 
${artifactId}-${version}. The term "finalName" is kind of a misnomer, 
however, as plugins that build the bundled project have every right to 
ignore/modify this name (but they usually do NOT). For example, if the 
maven-jar-plugin is configured to give a jar a classifier of test, then the 
actual jar defined above will be built as my-project-1.0-test.jar.

Basically it includes almost always the version in your .jar.

In the version (2.6 >), in the <configuration> it allows you to specify the <fileNameMapping>no-version</fileNameMapping>.



回答2:

The jar plugin alone is able to compute and write the manifest classpath. This produces a working jar with the desired name

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>