Maven classpath order issues
Asked Answered
T

4

44

Does anyone know of a way to set a specific classpath order in Maven2, rather than the random ordering I appear to experience at the moment?

There are a number of legitimate reasons for wanting to do this:

  • A vendor has supplied a patch jar, which contains overriding classes for a previously released jar and therefore the patch jar must appear first in the classpath ordering.
  • Two jar's found on the classpath discovered by traversing pom dependencies contain the same class in the same package with different signitures. For example:

jboss jbossall-client 4.2.0.GA

org.hibernate hibernate 3.1

both contain: org.hibernate.util.ReflectHelper.class, but the jbossall-client version is missing the getFastClass method.

From googling I see that this is perhaps a point of contention between maven enthusiasts and people facing this particular issue, but surely there are legitimate reasons for classpath ordering.

Any advice from anyone that has solved this particular quandary would be much appreciated!

Thanks

Tolkan answered 27/4, 2009 at 10:58 Comment(1)
if jboss contains hibernate at one version, surely you wouldn't want to mix with another version of hibernate?Ability
L
43

As of version 2.0.9 maven uses pom order for classpath, so you can actually manipulate it now. We mostly supress transitive dependencies to external libraries that we also include directly.

From the release notes of maven 2.0.9:

MNG-1412 / MNG-3111 introduced deterministic ordering of dependencies on the classpath. In the past, natural set ordering was used and this lead to odd results. The ordering is now preserved from your pom, with dependencies added by inheritence added last. In builds that had conflicting or duplicate dependencies, this may introduce a change to the output. In short, if you have weird issues with 2.0.9, take a look at the dependencies to see if you have conflicts somewhere.

Latialatices answered 27/4, 2009 at 12:0 Comment(0)
D
2

Maven 2.0.9 adds correct ordering so you absolutely must have that version or higher for the below to work.

Secondly you need the an updated plugin. The Maven guys are working on a fix, its in their jira to fix but this is something I urgently needed. So in the meantime I have fixed this myself and you can pull the Modified plugin source code from github.

Edit: Refer to http://jira.codehaus.org/browse/MECLIPSE-388

There are two ways to install it, either pull my modified code and install it or download the prebuilt jar and just add it.

Building the plugin

Run maven install from the plugin directory you checked out and then add the following in your plugins section of your projects pom:

<build>
  </plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-eclipse-plugin</artifactId>
      <version>2.8-cpfix</version>
    </plugin>
  </plugins>
</build>

Download the jar

Alternatively if you don't want to download and compile yourself then you can just get hold of the jar file and install it yourself.

Once you have the file run

mvn install:install-file -Dfile=<path-to-file> -DgroupId=org.apache.maven.plugins \
    -DartifactId=maven-eclipse-plugin -Dversion=2.8-cpfix -Dpackaging=jar

Regardless of how you installed it now when you run mvn eclipse:eclipse it will pick up the modified code and order the dependencies based on the order you defined in your pom file, no alphabetical ordering. It will also put the JRE container at the top of the dependencies.

Hopefully the real version of this code will come out soon, but in the meantime this fix has worked for me on my project and I hope it can help some others as well.

Digitalize answered 17/7, 2010 at 17:6 Comment(0)
T
1

Rather a further qualification of the question than an answer:
under "Maven Dependencies" Eclipse does not seem to honour the POM-order.
(it does use the POM-order under "Java Build Path" & in the Classpath)

Is that the expected behaviour?

I'm using Eclipse 2021-09 (which has Maven 3.8.1 embedded) under Windows 10.

Here's the POM:

<project
        xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.group</groupId>
    <artifactId>arty.fact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Maven Dependency Order</name>

    <properties>
        <maven.compiler.target>17</maven.compiler.target>
        <maven.compiler.source>17</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
            <exclusions><exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion></exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.14.1</version>
            <exclusions><exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion></exclusions>
        </dependency>
    </dependencies>
</project>

The Maven Dependencies looks like this:
Maven Dependencies

Talbott answered 26/10, 2022 at 22:33 Comment(0)
Z
0

If you have problem starting with IntelliJ IDEA, you can change the dependencies order from project structrue.

Zildjian answered 18/2, 2023 at 3:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.