1

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.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51

2 Answers2

2

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

King Reload
  • 2,780
  • 1
  • 17
  • 42
  • 1
    *"For you internal dependencies use true. For you 3rd party libraries use false."* I know how to define the group ID for my artifacts, but how do I configure the group ID in my POM for *all except my*? – Timothy Truckle Apr 27 '17 at 08:48
  • Except your 3rd party? Or what do you mean, as you stopped at `except my?`?? – King Reload Apr 27 '17 at 10:14
  • 1
    to match the external libs I would like to use a *wild card* excluding my own jars by their groupId. – Timothy Truckle Apr 27 '17 at 10:19
  • When `` subelements are present, they define a set of project coordinates to include. If none is present, then `` represents all valid values. Artifact coordinates may be given in simple `groupId:artifactId` form, or they may be fully qualified in the form `groupId:artifactId:type[:classifier]:version`. Additionally, wildcards can be used, as in `*:maven-*` – King Reload Apr 27 '17 at 10:37
  • 1
    Could you please copy the POM from my Quesston and enhance it with an example in your answer? – Timothy Truckle Apr 27 '17 at 10:40
  • You could also take a look at this: http://stackoverflow.com/a/13288677/7707749 to understand how to implement a wildcard – King Reload Apr 27 '17 at 10:46
  • this other answer has wildcarts for the folders, not for the goupIds... – Timothy Truckle Apr 27 '17 at 11:03
  • Currently I'm helping from my phone, so I can't do much, I do understand what you're saying now, as I was a bit confused of what you meant before. Maybe you could try to look into http://stackoverflow.com/a/7556707/7707749 for group id's and wildcards. I hope this could help you out better then my last link. – King Reload Apr 27 '17 at 11:13
  • I really apprechiate your effords, but those links refer to diffrent plugins/POM-parts, so I would really like to see an example suitable for my problem... – Timothy Truckle Apr 27 '17 at 11:21
  • Sadly I can't provide you of an example right now, as I dont have my computer near :( I hope your problem will be solved anyway! I am very curious in what way you mean to use the wildcard / groupid – King Reload Apr 27 '17 at 11:25
1

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>
dfogni
  • 763
  • 9
  • 14
  • 1
    Yes, but as I mentioned in the Question: the resulting classpath contains version numbers for all jars, even the jars of my own subprojects that have exactly this configuration (`${project.artifactId}`) in their POM. This is the problem. – Timothy Truckle Apr 28 '17 at 18:58