Maven tutorial diagnosis and tips

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.

You can force the update of the artifact that is already present in your local repository from the remote repository with “-U” flag.

#2. The Maven project you are trying to create, already exist in the current folder

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate (defaundalone-pom: Directory simpleBatch already exists – please run from a clean directory -> [Help 1]

To fix it: Delete the project (i.e. folder) that is already present.

#3. Maven does not automatically download a particular artifact from a repository

To fix it: In this case, you can manually download the jar file, and then install it to your local repository using the following command.

Example:

#4. My dependencies are not updated within eclipse

Run on a DOS command-line:

Within eclipse:

eclipse-maven-update-project

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

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.

eclipse-maven-hierachy

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.

To show the logical pom.xml or settings.xml being used

Note: options like help:active-profiles and help:all-profiles also come in handy.

To skip tests to resolve any other issues

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:

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”

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

[INFO] — maven-enforcer-plugin:1.4:enforce (enforce-rules) @ com.myapp-endpoint —
[WARNING] +-com.myapp-endpoint:0.0.1
+-com.mangofactory:swagger-springmvc:0.8.3
+-com.wordnik:swagger-core_2.10:1.3.2
+-commons-lang:commons-lang:2.4


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.


800+ Java Interview Q&As Menu

Learn by categories on the go...
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.
Top