Maven remove version from dependency jar

dejvid picture dejvid · Jun 15, 2016 · Viewed 8k times · Source

I'd like to know if there is a way to remove version number from maven dependency.

Let's say for my project I'd like to fetch commons-lang3 3.4 using maven dependency plugin:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

My pom configuration says, it is fetching dependencies to the ./lib directory inside of my project.
What I would like to achieve is remove on the fly version number from commons-lang3-3.4.jar. It would look like:

./lib/commons-lang3.jar

Question: Is there any way to do such thing?

Specifying finalName won't help here.

<build>
    <finalName>${project.name}-testing</finalName>
</build>

Below my existing configuration:

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${dir.javaLibs}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Answer

A_Di-Matteo picture A_Di-Matteo · Jun 16, 2016

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

Strip artifact version during copy

Which has default value to false.

Hence, given your existing configuration, here is the change:

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${dir.javaLibs}</outputDirectory>

                            <!-- new configuration entry below-->
                            <stripVersion>true</stripVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>