Q. In your experience, what are some of the challenges you faced with maven, and how do you over come them? A. When it works which is 95% of the time it’s quite nice, and when it doesn’t, it can be a challenge. Some of the challenges can be resolved by keeping some tips in mind. Most often it is due to the lack of understanding of how Maven works.
#1. Desired Maven archetype or artifact does not exist
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate (default-cli) on project standalone-pom: The desired archetype does not exist (org.apache.maven.archetypes:maven-archetype-quickstart:RELEASE) -> [Help 1]
Q. Where can you find the maven plugins and archetypes? A. ~/.m2/repository/org/apache/maven/plugins and ~/.m2/repository/org/apache/maven/plugins/archetypes.
To fix it: delete the “archetypes” folder and rerun the mvn command. Same course of action for any other artifacts that are missing.
#4. My dependencies are not updated within eclipse
Run on a DOS command-line:
1
mvn eclipse:clean eclipse:eclipse
Within eclipse:
#5.Limited visibility into the dependencies (i.e. transitive dependencies)
You have no control over, and limited visibility into, the dependencies specified by your dependencies (i.e. transitive dependencies). Builds will break because different copies of Maven will download different artifacts at different times. Your local build can break again in the future when the dependencies of your dependencies accidentally release new, non-backwards compatible changes without remembering to bump their version number. You can use the following commands and tools to check the dependency tree, and exclude certain artifact versions in the pom.xml file.
1
2
mvn dependency:tree
mvn dependency:analyze
Eclipse has a graphical tool to analyze this. Double click on the pom.xml file to open the editor, and select the “Dependency Hierarchy” tab. You can filter for your artifact and its transitively resolved dependencies to apply the exclusion in your pom.xml file.
This is also very handy to resolve class loading issues due to jar conflicts. Also keep in mind that the dependencies are loaded in the order in which they are declared in the pom.xml. So look at the effective pom with mvn help:effective-pom.
The following flags are handy for debugging any other maven issues:
The -X flag shows debug logging, and tee it to a file which you then search for clues of what happened.
1
mvn-Xinstall|tee maven_out.txt
To show the logical pom.xml or settings.xml being used
1
2
mvn help:effective-pom
mvn help:effective-settings
Note: options like help:active-profiles and help:all-profiles also come in handy.
To skip tests to resolve any other issues
1
2
3
4
5
mvn install-DskipTests//this will skip running tests, but still compile it.
mvn install-Dmaven.test.skip=true//this will skip compilation and execution of tests
mvn install-Dtest=FooTest//runs only FooTest
mvn-Dmaven.surefire.debug test//to debug your tests
There are other handy options to resume your build from where maven failed in a multi module project. For example, if your build failed at proj-commons, you can run the build from this module by typing:
1
2
3
4
mvn-rf proj-commons clean install//Resume from proj-commons
mvn-pl proj-commons,proj-service clean install//build only 2 modules
mvn-nsu clean install//Don't update the snapshots
rf –> resume from, pl –> project list, nsu –> no snapshot update, am –> also make (If project list is specified, also build projects required by the list), amd –> also make dependents (If project list is specified, also build projects that depend on projects on the list)
To debug maven plugins via your IDE like eclipse,set the MAVEN_OPTS environmental variable as shown below:
How to exclude JARs in your pom.xml file to fix “JAR Hell issues”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<dependency>
<groupId>com.myapp</groupId>
<artifactId>some-app</artifactId>
<version>${myapp.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</exclusion>
...
</exclusions>
</dependency>
You normally exclude the jars brought in via the “transitive dependency” and add the specific versions you are interested in explicitly in the pom.xml file.
The error you normally get is from the enforcer plugin
and
+-com.myapp-service:0.0.1
+-com.mgl.wrap.iliquid.decs:iliquid-decs-service:0.0.1-PEGAINTEGRATION-SNAPSHOT
+-net.sf.jasperreports:jasperreports:6.0.4
+-org.codehaus.castor:castor-xml:1.3.3
+-commons-lang:commons-lang:2.6
You need to pick either version 2.4 or 2.6. Can’t have both. Fix is to exclude one of the versions. In this case the 2.6 version brought in by the net.sf.jasperreports:jasperreports:6.0.4 is excluded.
Learn by categories such as FAQs – Core Java, Key Area – Low Latency, Core Java – Java 8, JEE – Microservices, Big Data – NoSQL, Architecture – Distributed, Big Data – Spark, etc. Some posts belong to multiple categories.