Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven remove version from dependency jar

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>
like image 403
dejvid Avatar asked Jun 15 '16 14:06

dejvid


People also ask

How to exclude dependency of dependency maven?

Open the dependency POM and find the transitive dependency you want to exclude. Copy groupId and artifactId . In your project POM, underneath your active dependency, enter exclusions and using code completion paste the copied info of the dependency you want to exclude.

What is exclusion in dependency?

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.

How do I remove a dependency in Maven?

Open the pom file and click on dependency Hierarchy. Select the the jar you want to delete. Right click and click on Exclude Maven Artifact.

How to make maven dependency optional?

In order to exclude these special dependencies from the main project, we can apply Maven's <optional> tag to them. This forces any user who wants to use those dependencies to declare them explicitly. However, it does not force those dependencies into a project that doesn't need them.


1 Answers

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>
like image 95
A_Di-Matteo Avatar answered Oct 23 '22 04:10

A_Di-Matteo