Top 10 Maven questions by Java developers - Top 10 Maven questions- part 2

 

5) How can I manually install a library  to Maven repository?

Due the binary license, some libraries could not available in public repository. For example, there is no public repository with the Oracle Driver JAR.

So how do you overcome with this issue ? Once you've downloaded the JAR just add it to your computer repository with (note I pulled the groupId, artifactId and version from the POM):

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 \
     -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc.jar -DgeneratePom=true

The last parameter for generating a POM will save you from pom.xml warnings

6) Cannot find the web.xml file:

Error assembling WAR: webxml attribute is required
This is a very frequent issue that you could meet when you try to deploy a web application. This happens if you forgot to include the web.xml file in your Web application or that maybe it's not in the location where Maven war plugin expects to find it.
In either case you can fix it by stating explicitely where the web.xml is. For example, here is where Maven will look fr web.xml by default:

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
 </build>

7) How to run a Java class using Maven ?

Executing a Java class can be done in a few ways. If you do not want to add anything to your configuration, you could use the maven-exec-plugin and adding parameters on the command line. Example:

mvn exec:java -Dexec.mainClass="com.example.Main"

This will run your class in the JVM.

If you're doing this regularly, you can add the parameters into the pom.xml as well:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>com.example.Main</mainClass>
    <arguments>
      <argument>foo</argument>
      <argument>bar</argument>
    </arguments>
  </configuration>
</plugin>

8) How do I remove unused dependencies in your project?

The simplest and recommended way to find the used and unused dependencies in your project is by running the Maven dependency:analyze goal. The dependency:analyze analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared.

[WARNING] Used undeclared dependencies found:
[WARNING]    javax.inject:javax.inject:jar:1:compile
[WARNING] Unused declared dependencies found:
[WARNING]    org.jboss.spec.javax.ejb:jboss-ejb-api_3.2_spec:jar:1.0.0.Final:pro
vided
[WARNING]    javax.validation:validation-api:jar:1.0.0.GA:compile
[WARNING]    javax.enterprise:cdi-api:jar:1.1:provided

9) How to change the Java version used by Maven to compile ?

By default Maven will use JAVA_HOME to determine which Java version to use. You can check the actual Maven configuration by issuing:

mvn clean --debug

Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00)
Maven home: C:\apache-maven-3.2.1\bin\..
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: C:\Java\jdk1.7.0_51\jre

You should as well include in your maven compiler plugin a version which matches with the value for your Java version:

    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>

10) How to include a dependency from a file system path (instead of Maven repository) ?

Althoug Maven is built around the concept of Repository, you can have occasionally the need to reference libraries which are contained in a standard file system. In order to do that, include the systemPath element in your dependency as in the following example:

<dependency>
      <groupId>samexampleple</groupId>  
       <artifactId>com.example.lib</artifactId>  
       <version>1.0</version>
      <scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/mylib.jar</systemPath>
</dependency>

References: http://stackoverflow.com/questions/1074869/find-oracle-jdbc-driver-in-maven-repository
http://stackoverflow.com/questions/5901378/what-exactly-is-a-maven-snapshot-and-why-do-we-need-it
http://stackoverflow.com/questions/6178583/maven-does-not-find-junit-tests-to-run
http://stackoverflow.com/questions/9846046/run-main-class-of-maven-project
http://stackoverflow.com/questions/7456006/maven-packaging-without-test-skip-tests

 

Francesco Google+