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:
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 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
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 :
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:
To a local file URL:
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:
<dependency> <!--from a repository-->
<dependency> <!--from a system path-->
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:
- 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"
- Copy your repository folder onto your target computer, where you do not have any Internet access
- On your target computer, use the default maven settings.xml
- 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:
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).
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:
<!-- including properties files -->
<!-- excluding non-properties e.g. bin files
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
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.
Distribution management manages the distribution of the artifact and supporting files generated throughout the build process. The configuration below
More useful links: