Menu Search Sign up

Maven

Download and install

Eclipse already has embedded maven, and you can use it to build your applicaiton from within eclipse (See below sections). But you cannot run it from CLI.

To run maven from CLI, you need to download and install Maven. Here are the steps:

1. https://maven.apache.org/download.cgi?Preferred=ftp%3A%2F%2Fmirror.reverse.net%2Fpub%2Fapache%2F
2. download binary zip http://apache.mirror.globo.tech/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.zip
3. Unzip the zip to any folder.
4. Make sure JDK 1.7 (not JRE) or above is installed, and “JAVA_HOME” variable is added as Windows environment variable, and it points to your JDK home folder, like C:\kk\Java\jdk1.8.0_171.
5. Update PATH variable, append Maven bin folder (like C:\kk\apache-maven-3.6.0\bin), so that you can run the Maven’s command everywhere, and of course, ensure Java bin folder (like /opt/jdk1.8.0_181/jre/bin) is in your PATH variable.
6. Done, to verify it, run mvn –version in the command prompt.
7. To build a pacakge, navigate to the place where you have your pom.xml file, then run mvn package

Once installed, when running Maven, all dependency jar files will be downloaded to <user home>/.m2/repository.

For advanced users, you can also manipulate the settings.xml file in <maven installation folder>/conf/

You can also place the settings.xml file under <user home>/.m2

Maven Build Goals

Here is a list, in the sequence of build lifecyle, of Maven build. Each one goal is included by the one after it:

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • verify - run any checks on results of integration tests to ensure quality criteria are met
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

Maven repository

http://repo.maven.apache.org/maven2/

Maven and Eclipse

Eclipse - Eclipse Java EE IDE for Web Developers, Version: Neon.3 Release (4.6.3) - has an embedded Maven.

Right click a maven project -> Run As -> Maven Build ... -> In the pop-up window, you will see the embedded maven version.

In addition, you can also specify the "clean" goal to clean up the target before you build on any of the above goals.

To build a Maven project on any of the above goals in Eclipse: right click the Maven project ->  Run As -> Maven build ... -> in the pop-up window, specify the Goals (you can specify more than 1 goals separated by space) -> click "Run"

To clean a Maven project in Eclipse: right click the Maven project ->  Run As -> Maven build clean

To compile, test (with junit test cases under src/test/java), and install (into maven distribution repository) a maven project in eclipse: right click the Maven project ->  Run As -> Maven build install

What to specify in Eclipse "Java Build Path" of the Maven project?
1. JRE system library (1.6, 1.7, 1.8, etc.)

2. Maven Dependencies (Eclipse will populate these dependencies based on your pom.xml)

Creating Maven Ear Project with Eclipse

http://javahonk.com/create-maven-ear-project-eclipse/  

Other

http://theopentutorials.com/examples/java-ee/ejb3/how-to-create-a-ejb-3-x-project-using-maven-in-eclipse-part-1/

How to convert Maven based Java Project to support Eclipse IDE

It’s really easy to do it. Navigate to your Java project folder, where pom.xml file is placed. And issue this command :

mvn eclipse:eclipse

Maven repository, local repository, and local system path

You can specify your repositories to point to a remote URL or to a local file URL (this is not as straightforward as what I outlined in section "Run Maven Completely Locally"):

To a remote URL:

  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>

To a local file URL:

  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>file://C:/Users/myname/.m2/repository</url>
    </repository>
  </repositories>

When you specify your dependencies, you can specify a system path so the artifacts that your build depend on will be retrieved from a local system path, instead of a repository:

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency> <!--from a repository-->
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
            <scope>compile</scope>
        </dependency>
           <dependency> <!--from a system path-->
            <groupId>org.apache</groupId>
              <artifactId>commons-codec</artifactId>
              <version>1.10</version>
              <scope>system</scope>
              <systemPath>C:\Users\myname\Downloads\commons-codec-1.10-bin\commons-codec-1.10\commons-codec-1.10.jar</systemPath>
        </dependency>
    </dependencies>    
For more information on maven repository, go to https://maven.apache.org/guides/mini/guide-multiple-repositories.html

Run Maven Completely Locally

If your company policy blocks you from downloading dependencies from the Internet, you can:

  1. Run your Maven project once with access to the Internet - this will download all dependent jar files under <user home>/.m2/repository, with commands like "mvn clean install -DskipTests=true"
  2. Copy your repository folder onto your target computer, where you do not have any Internet access
  3. On your target computer, use the default maven settings.xml
  4. Run the same mvn command, adding the -o option: mvn -o clean install -DskipTests=true

In your pom.xml, you only need <dependencies> section and simple <build><plugins> section like below:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>

If you still run into issues, like some dependencies cannot be resolved, etc., you can clean up your .m2 folder, and run your Maven project again (from your computer with Internet access). 

POM Examples

Including / Excluding Resources

By default, all resource files under src/main/resources will be copied to target/classes. If you want to selectively include / exclude some resource files:

         <resources>
            <!-- including properties files -->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.props</include>
                    <include>**/*.txt</include>
                    <include>**/*.sh</include>
                </includes>
            </resource>
            <!-- excluding non-properties e.g. bin files 
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>**/*.props</exclude>
                    <exclude>**/*.xml</exclude>
                </excludes>
            </resource>-->
        </resources>

A sample maven project for standalone java application (non-ejb)
Refer to ConsentLoader

A sample maven project (with parent+modules) for jax-rs / jax-ws java application
Refer to ConsentMgmtParent

How to use Maven to build a jar with all dependency jar files

https://crunchify.com/how-to-create-build-java-project-including-all-dependencies-using-maven-maven-resources-maven-dependency-maven-jar-plugin-tutorial/

Others 

Tutorial

https://maven.apache.org/guides/introduction/introduction-to-the-pom.html 

Repository vs Plugin Repository

A plugin is a tool you use at the execution of your maven build. For example, the compiler plugin compiles your Java code into Bytecode.

Back in Maven1.0, plugins and normal artifacts were in different repositories (as far as I know). In Maven2+ they are stored together in 1 repository.

  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>      
      <url>https://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>

Distribution Management

Distribution management manages the distribution of the artifact and supporting files generated throughout the build process. The configuration below 

    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>Internal Releases</name>
            <url>file://C:/Users/myname/.m2/repository</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>Internal Snapshots</name>
            <url>file://C:/Users/myname/.m2/repository</url>            
        </snapshotRepository>
    </distributionManagement>

More useful links:

https://repo.maven.apache.org/maven2/org/apache/maven/plugins/

https://stackoverflow.com/questions/17625496/maven-local-repository-in-settings-xml-vs-pom-xml



https://www.mkyong.com/maven/how-to-create-a-jar-file-with-maven/