This link has been bookmarked by 174 people . It was first bookmarked on 09 Sep 2006, by Crm Revolution Proyect.
-
06 May 15
-
The scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime.
-
With this information about a dependency, Maven will be able to reference the dependency when it builds the project. Where does Maven reference the dependency from? Maven looks in your local repository (~/.m2/repository is the default location) to find all dependencies.
-
Whenever a project references a dependency that isn't available in the local repository, Maven will download the dependency from a remote repository into the local repository.
-
By default, the remote repository Maven uses can be found (and browsed) at http://repo.maven.apache.org/maven2/
-
Let's say we've added some logging to the code and need to add log4j as a dependency. First, we need to know what the groupId, artifactId, and version are for log4j. We can browse ibiblio and look for it, or use Google to help by searching for "site:www.ibiblio.org maven2 log4j". The search shows a directory called /maven2/log4j/log4j (or /pub/packages/maven2/log4j/log4j). In that directory is a file called maven-metadata.xml. Here's what the maven-metadata.xml for log4j looks like:
-
-
03 Mar 15
-
Maven Getting Started Guide
-
-
06 Nov 14
-
In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements.
-
-
27 Oct 14
-
Maven Getting Started Guide
This guide is intended as a reference for those working with Maven for the first time
-
-
15 Sep 14
-
Change to the directory where pom.xml is created
-
Before compiling and executing the tests Maven compiles the main code
-
If you simply want to compile your test sources (but not execute the tests),
-
Making a JAR file
-
install the artifact you've generated (the JAR file) in your local repository (~/.m2/repository is the default location)
-
generate a web site for your project!
-
This will remove the target directory with all the build data before starting so that it is fresh.
-
To find out what configuration is available for a plugin, you can see the Plugins List and navigate to the plugin and goal you are using. For general information about how to configure the available parameters of a plugin, have a look at the Guide to Configuring Plug-ins.
-
The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.
-
Sometimes a resource file will need to contain a value that can only be supplied at build time.
-
You can also set up your own remote repository (maybe a central repository for your company) to use instead of or in addition to the default remote repository.
-
-
17 Jun 14
-
10 Jun 14
-
a project management and comprehension tool and as such provides a way to help with managing:
-
ndicates the unique base name of the primary artifact being generated by this project.
-
<artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
-
his will remove the target directory with all the build data before starting so that it is fresh.
-
any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.
-
groupId, artifactId, version, and scope.
-
he scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime.
-
-
04 Jun 14
-
Maven's archetype mechanism
-
riginal pattern or model
-
n Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements.
-
file named pom.xml
-
pom.xml contains the Project Object Model (POM) for this project.
-
This is a very simple POM but still displays
-
the key elements every POM contains
-
After the archetype generation of your first project you will also notice that the following directory structure has been created:
-
the project created from the archetype has a POM, a source tree for your application's sources and a source tree for your test sources.
-
How do I compile my application sources?
-
mvn compile
-
The first time you execute this (or any other) command, Maven will need to download all the plugins and related dependencies it needs to fulfill the command.
-
the compiled classes were placed in ${basedir}/target/classes,
-
compile my test sources and run my unit tests?
-
mvn test
-
create a JAR and install it in my local repository?
-
mvn package
-
ou can now take a look in the ${basedir}/target directory and you will see the generated JAR file.
-
mvn install
-
mvn clean
-
This will remove the target directory with all the build data before starting so that it is fresh.
-
use external dependencies?
-
lists all of the external dependencies that our project needs in order to build (
-
The groupId, artifactId, and version are the same as those given in the pom.xml for the project that built that dependency.
-
For each external dependency, you'll need to define at least 4 things: groupId, artifactId, version, and scope.
-
he scope element indicates how your project uses that dependency, and can be values like compile, test, and runtim
-
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> <scope>compile</scope> </dependency>
-
-
24 Mar 14
-
05 Mar 14
-
An archetype is defined as an original pattern or model from which all other things of the same kind are made. In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements. We are going to show you how the archetype mechanism works now, but if you would like to know more about archetypes please refer to our Introduction to Archetypes.
-
Maven's archetype mechanism
-
mvn archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DgroupId=com.mycompany.app \ -DartifactId=my-app
-
version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.
-
As you can see from the output, the compiled classes were placed in ${basedir}/target/classes, which is another standard convention employed by Maven.
-
C:\Test\Maven2\test\my-app\target\test-classes
-
- Before compiling and executing the tests Maven compiles the main code (all these classes are up to date because we haven't changed anything since we compiled last).
-
mvn test-compile
-
There are plenty of other standalone goals that can be executed as well, for example:
mvn clean
This will remove the target directory with all the build data before starting so that it is fresh.
-
This can be run over the top of a previous IDEA project - it will update the settings rather than starting fresh.
If you are using Eclipse IDE, just call:
<!-- TODO: need a sidebar notation for notes like this -->mvn eclipse:eclipse
Note: some familiar goals from Maven 1.0 are still there - such as jar:jar, but they might not behave like you'd expect. Presently, jar:jar will not recompile sources - it will simply just create a JAR from the target/classes directory, under the assumption everything else had already been done.
-
The configuration element applies the given parameters to every goal from the compiler plugin. In the above case, the compiler plugin is already used as part of the build process and this just changes the configuration. It is also possible to add new goals to the process, and configure specific goals. For information on this, see the Introduction to the Build Lifecycle.
-
You see below in our example we have added the directory ${basedir}/src/main/resources into which we place any resources we wish to package in our JAR. The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.
-
So you can see in our example that
-
sedir}/src/main/resources can be found starting at the base of the JAR and our application.properties file is there in the META-INF directory. You will also notice some other files there like META-INF/MANIFEST.MF as well as a pom.xml and pom.properties file. These come standard with generation of a JAR in Maven. You can create your own manifest if you choose, but Maven will generate one by default if you don't. (You can also modify the entries in the d
-
Operating on the POM file would require you to use some Maven utilities but the properties can be utilized using the standard Java API and look like the following:
-
pattern
-
// Retrieve resource InputStream is = getClass().getResourceAsStream( "/test.properties" ); // Do something with the resource ...
-
<filtering>true</filtering>
-
Maven
-
To reference a property defined in an external file, all you need to do is add a reference to this external file in your pom.xml. First, let's create our external properties file and call it src/main/filters/filter.properties:
-
filters
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Maven Quick Start Archetype</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
-
From this file, we can see that the groupId we want is "log4j" and the artifactId is "log4j". We see lots of different version values to choose from; for now, we'll just use the latest version, 1.2.12 (some maven-metadata.xml files may also specify which version is the current release version). Alongside the maven-metadata.xml file, we can see a directory corresponding to each version of the log4j library. Inside each of these, we'll find the actual jar file (e.g. log4j-1.2.12.jar) as well as a pom file (this is the pom.xml for the dependency, indicating any further dependencies it might have and other information) and another maven-metadata.xml file. There's also an md5 file corresponding to each of these, which contains an MD5 hash for these files. You can use this to authenticate the library or to figure out which version of a particular library you may be using already.
-
-
04 Nov 13
-
31 Oct 13
-
23 Oct 13
-
To reference a property defined in your pom.xml, the property name uses the names of the XML elements that define the value, with "pom" being allowed as an alias for the project (root) element. So ${pom.name} refers to the name of the project, ${pom.version} refers to the version of the project, ${pom.build.finalName} refers to the final name of the file created when the built project is packaged, etc. Note that some elements of the POM have default values, so don't need to be explicitly defined in your pom.xml for the values to be available here. Similarly, values in the user's settings.xml can be referenced using property names beginning with "settings" (for example, ${settings.localRepository} refers to the path of the user's local repository).
-
<filters> <filter>src/main/filters/filter.properties</filter> </filters>
-
-
30 Sep 13
-
creating your first Maven project
-
An archetype is defined as an original pattern or model from which all other things of the same kind are made
-
mvn archetype:generate
-
pom.xml contains the Project Object Model (POM) for this project
-
groupId This element indicates the unique identifier of the organization or group that created the project
-
artifactId This element indicates the unique base name of the primary artifact being generated by this project
-
The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name.
-
packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.).
-
version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development
-
name This element indicates the display name used for the project.
-
url This element indicates where the project's site can be found.
-
description This element provides a basic description of your project
-
Making a JAR file is straight forward enough and can be accomplished by executing the following command:
<!-- How to skip tests ... jvz -->mvn package
-
Now you'll want to install the artifact you've generated (the JAR file) in your local repository (~/.m2/repository is the default location)
-
mvn install
-
-
07 Aug 13
-
Maven will, of course, work for small projects, but Maven shines in helping teams operate more effectively by allowing team members to focus on what the stakeholders of a project require. You can leave the build infrastructure to Maven!
-
Maven is an attempt to apply patterns to a project's build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices
-
a way to help with managing:
-
employing standard conventions and practices to accelerate your development cycle
-
An archetype is defined as an original pattern or model from which all other things of the same kind are made. In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements.
-
the key elements every POM contains
-
If you simply want to compile your test sources (but not execute the tests)
-
IntelliJ IDEA descriptor
-
-
27 Jul 13
-
POM Reference
-
In the above case
-
applies the given parameters
-
configuration
-
-
31 May 13
-
resources
-
<parent>
-
also intended to serve as a cookbook with self-contained references and solutions for common use cases
-
step through the material in a sequential fashion
-
Maven is essentially a project management and comprehension tool
-
accelerate your development cycle
-
org.apache.maven.archetypes
-
<version>3.8.1</version>
-
it is mandatory in order to ensure stability of use
-
he display name used for the project
-
This is often used in Maven's generated documentation
-
where the project's site can be found
-
This is the standard layout for Maven projects
-
Now you'll want to install the artifact you've generated (the JAR file) in your local repository (~/.m2/repository is the default location).
-
mvn install
-
surefire plugin
-
This will remove the target directory with all the build data before starting so that it is fresh.
-
Whenever you want to customise the build for a Maven project, this is done by adding or reconfiguring plugins.
-
For this example, we will configure the Java compiler to allow JDK 5.0 sources. This is as simple as adding this to your POM:
-
<configuration>
-
This plugin will be automatically downloaded and used
-
all plugins in Maven 2.0 look much like a dependency
-
The configuration element applies the given parameters to every goal from the compiler plugin. In the above case, the compiler plugin is already used as part of the build process and this just changes the configuration
-
packaging resources in the JAR file
-
${basedir}/src/main/resources
-
-- main
-
resources
-
META-INF directory
-
application.properties
-
maven
-
some other files there like META-INF/MANIFEST.MF as well as a pom.xml and pom.properties file. These come standard with generation of a JAR in Maven
-
manifest if you choose, but Maven will generate one by default if you don't.
-
resources `-- test.properties
-
getClass().getResourceAsStream( "/test.properties" );
-
Sometimes a resource file will need to contain a value that can only be supplied at build time
-
the values defined in your pom.xml
-
The property
-
a value defined in the user's settings.xml,
-
a property defined in an external properties file, or a system property
-
<build>
-
<filtering>true</filtering>
-
we had to explicitly state that the resources are located in the src/main/resources directory
-
order to override that default value and set filtering to true
-
To reference a property defined in your pom.xm
-
with "pom" being allowed as an alias for the project (root) element.
-
# application.properties
-
To reference a property defined in an external file,
-
src/main/filters/filter.properties:
-
# filter.properties
-
<filters> <filter>src/main/filters/filter.properties</filter> </filters>
-
<filtering>true</filtering>
-
The dependencies section of the pom.xml lists all of the external dependencies that our project needs in order to build
-
test time
-
run time
-
compile time
-
For each external dependency, you'll need to define at least 4 things: groupId, artifactId, version, and scope.
-
The scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime
-
aven looks in your local repository (~/.m2/repository is the default location) to find all dependencies
-
Once it's installed there, another project can reference that jar as a dependency simply by adding the dependency information to its pom.xml
-
<artifactId>my-other-app</artifactId>
-
<dependency>
-
What about dependencies built somewhere else? How do they get into my local repository?
-
Whenever a project references a dependency that isn't available in the local repository, Maven will download the dependency from a remote repository into the local repository
-
In that directory is a file called maven-metadata.xml
-
<metadata>
-
For deploying jars to an external repository, you have to configure the repository url in the pom.xml and the authentication information for connectiong to the repository in the settings.xml.
-
<project
-
<filter>src/main/filters/filters.properties</filter>
-
<resource>
-
<distributionManagement>
-
<settings
-
use the archetype mechanism to generate a site
-
archetypes
-
archetypeArtifactId
-
archetypeArtifactId=maven-archetype-webapp
-
<artifactId>my-webapp</artifactId>
-
<packaging>war</packaging>
-
<finalName>my-webapp</finalName>
-
package
-
multiple modules
-
The POM file you'll create should contain the following:
-
<packaging>pom</packaging>
-
<modules>
-
so add this to my-webapp/pom.xml:
-
We'll need a dependency on the JAR from the webapp,
-
<parent>
-
The WAR has now been created in my-webapp/target/my-webapp.war, and the JAR is included:
-
This tells Maven to run all operations over the set of projects instead of just the current one
-
has a packaging of pom and a list of modules defined
-
Next, we tell the WAR that it requires the my-app JAR
-
The reason for this is the <scope>test</scope> element - it is only required for testing, and so is not included in the web application as the compile time dependency my-app is.
-
The final step was to include a parent definition
-
-
04 Apr 13
-
13 Feb 13
-
01 Feb 13
-
compile
-
package
-
packaging element is set to jar
-
in the ${basedir}/target directory
-
generated (the JAR file) in your local repository
-
install
-
The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.
-
First, we need to know what the groupId, artifactId, and version are for log4j. We can browse ibiblio and look for it
-
maven-metadata.xml
-
deploy
-
For deploying jars to an external repository, you have to configure the repository url in the pom.xml and the authentication information for connectiong to the repository in the settings.xml.
-
modules
-
dependency
-
parent
-
-
08 Jan 13
-
31 Dec 12
-
11 Oct 12
-
27 Sep 12
-
12 Sep 12
-
-
This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
-
your local repository (~/.m2/repository is the default location).
-
If you are using Eclipse IDE, just call:
mvn eclipse:eclipse
-
-
04 Sep 12
-
05 Aug 12
-
How do I use plug-ins?
-
Maven Getting Started Guide
-
Maven for the first time
-
- What is Maven?
- How can Maven benefit my development process?
- How do I setup Maven?
- How do I make my first Maven project?
- How do I compile my application sources?
- How do I compile my test sources and run my unit tests?
- How do I create a JAR and install it in my local repository?
- How do I use plug-ins?
- How do I add resources to my JAR?
- How do I filter resource files?
- How do I use external dependencies?
- How do I deploy my jar in my remote repository?
- How do I create documentation?
- How do I build other types of projects?
- How do I build more than one project at once?
Sections
-
What is Maven?
-
a project management and comprehension tool
-
- Builds
- Documentation
- Reporting
- Dependencies
- SCMs
- Releases
- Distribution
help with managing:
-
How can Maven benefit my development process?
-
standard conventions and practices to accelerate your development cycle
-
How do I setup Maven?
-
defaults for Maven are often sufficient
-
cache location or are behind a HTTP proxy, you will need to create configuration
-
How do I make my first Maven project?
-
archetype
-
An archetype is defined as an original pattern or model from which all other things of the same kind are made.
-
mvn archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DgroupId=com.mycompany.app \ -DartifactId=my-app
-
a directory named my-app has been created
-
this directory contains a file named pom.xml
-
pom.xml contains the Project Object Model (POM) for this project
-
key elements every POM contains,
-
project
-
modelVersion
-
groupId This element indicates the unique identifier of the organization or group that created the project.
-
artifactId This element indicates the unique base name of the primary artifact being generated by this project.
-
packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.).
-
version This element indicates the version of the artifact generated by the project.
-
name This element indicates the display name used for the project
-
url This element indicates where the project's site can be found. T
-
description This element provides a basic description of your project.
-
How do I compile my application sources?
-
first time you execute this (or any other) command, Maven will need to download all the plugins and related dependencies it needs to fulfill the command.
-
compiled classes were placed in ${basedir}/target/classes, which is another standard convention employed by Maven.
-
How do I compile my test sources and run my unit tests?
-
mvn test
-
mvn test-compile
-
How do I create a JAR and install it in my local repository?
-
mvn package
-
mvn install
-
- **/*Test.java
- **/Test*.java
- **/*TestCase.java
- **/Abstract*Test.java
- **/Abstract*TestCase.java
Note that the surefire plugin (which executes the test) looks for tests contained in files with a particular naming convention. By default the tests included are:
And the default excludes are:
-
mvn idea:idea
This can be run over the top of a previous IDEA project - it will update the settings rather than starting fresh.
-
How do I use plug-ins?
-
For this example, we will configure the Java compiler to allow JDK 5.0 sources.
-
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build>
-
How do I add resources to my JAR?
-
Maven again relies on the Standard Directory Layout, which means by using standard Maven conventions you can package resources within JARs simply by placing those resources in a standard directory structure.
-
${basedir}/src/main/resources
-
You will also notice some other files there like META-INF/MANIFEST.MF as well as a pom.xml and pom.properties file. These come standard with generation of a JAR in Maven. You can create your own manifest if you choose, but Maven will generate one by default if you don't. (You can also modify the entries in the default manifest. We will touch on this later.) The pom.xml and pom.properties files are packaged up in the JAR so that each artifact produced by Maven is self-describing and also allows you to utilize the metadata in your own application if the need arises.
-
How do I filter resource files?
-
Sometimes a resource file will need to contain a value that can only be supplied at build time.
-
value into your resource file using the syntax ${<property name>}.
-
The property can be one of the values defined in your pom.xml, a value defined in the user's settings.xml, a property defined in an external properties file, or a system property.
-
<filtering>true</filtering>
-
To reference a property defined in an external file, all you need to do is add a reference to this external file in your pom.xml. First, let's create our external properties file and call it src/main/filters/filter.properties:
-
Next, we'll add a reference to this new file in the pom.xml:
-
<build> <filters> <filter>src/main/filters/filter.properties</filter> </filters>
-
Filtering resources can also get values from system properties;
-
mvn process-resources "-Dcommand.line.prop=hello again"
-
How do I use external dependencies?
-
The dependencies section of the pom.xml lists all of the external dependencies that our project needs in order to build
-
For each external dependency, you'll need to define at least 4 things: groupId, artifactId, version, and scope. The groupId, artifactId, and version are the same as those given in the pom.xml for the project that built that dependency. T
-
he scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime.
-
With this information about a dependency, Maven will be able to reference the dependency when it builds the project. Where does Maven reference the dependency from? Maven looks in your local repository (~/.m2/repository is the default location) to find all dependencies.
-
What about dependencies built somewhere else? How do they get into my local repository? Whenever a project references a dependency that isn't available in the local repository, Maven will download the dependency from a remote repository into the local repository.
-
You can also set up your own remote repository
-
How do I deploy my jar in my remote repository?
-
you have to configure the repository url in the pom.xml and the authentication information for connectiong to the repository in the settings.xml.
-
example using scp and username/password authentication:
-
<distributionManagement> <repository> <id>mycompany-repository</id> <name>MyCompany Repository</name> <url>scp://repository.mycompany.com/repository/maven2</url> </repository> </distributionManagement>
-
<servers> <server> <id>mycompany-repository</id> <username>jvanzyl</username> <!-- Default value is ~/.ssh/id_dsa --> <privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa) <passphrase>my_key_passphrase</passphrase> </server> </servers>
-
How do I build more than one project at once?
-
Firstly, we need to add a parent pom.xml file in the directory above the other two
-
The POM file you'll create should contain the following:
-
<modules> <module>my-app</module> <module>my-webapp</module> </modules>
-
add this to my-webapp/pom.xml:
-
<dependencies> <dependency> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> </dependency> ... </dependencies>
-
add the following <parent> element to both of the other pom.xml files in the subdirectories:
-
<parent> <groupId>com.mycompany.app</groupId> <artifactId>app</artifactId> <version>1.0-SNAPSHOT</version> </parent> ...
-
You might like to generate your IDEA workspace again from the top level directory.
-
mvn idea:idea
-
-
13 Jul 12
-
apply patterns to a project's build infrastructure
-
Maven's archetype mechanism
-
an original pattern or model from which all other things of the same kind are made.
-
-
12 Jul 12
-
nutshell Maven is an attempt to apply patterns to a project's build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices
-
Maven can provide benefits for your build process by employing standard conventions and practices to accelerate your development cycle while at the same time helping you achieve a higher rate of success
-
-
07 Jun 12
-
in a nutshell Maven is an attempt to apply patterns to a project's build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices.
-
a project management and comprehension tool
-
-
15 May 12
-
06 May 12
-
archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements
-
Project Object Model (POM)
-
groupId This element indicates the unique identifier of the organization or group that created the project
-
artifactId This element indicates the unique base name of the primary artifact being generated by this project
-
A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
-
packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.)
-
version This element indicates the version of the artifact generated by the project
-
name This element indicates the display name used for the project. This is often used in Maven's generated documentation.
-
url This element indicates where the project's site can be found
-
description This element provides a basic description of your project. This is often used in Maven's generated documentation.
-
You will most likely want to customize your Maven site but if you're pressed for time all you need to do to provide basic information about your project is execute the following command:
mvn site
-
- **/*Test.java
- **/Test*.java
- **/*TestCase.java
- **/Abstract*Test.java
- **/Abstract*TestCase.java
Note that the surefire plugin (which executes the test) looks for tests contained in files with a particular naming convention. By default the tests included are:
And the default excludes are:
-
The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.
-
Sometimes a resource file will need to contain a value that can only be supplied at build time. To accomplish this in Maven, put a reference to the property that will contain the value into your resource file using the syntax ${<property name>}.
-
To have Maven filter resources when copying, simply set filtering to true for the resource directory in your pom.xml:
-
So ${pom.name} refers to the name of the project, ${pom.version} refers to the version of the project, ${pom.build.finalName} refers to the final name of the file created when the built project is packaged
-
The scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime.
-
By default, the remote repository Maven uses can be found (and browsed) at http://repo.maven.apache.org/maven2/
-
The concept of dealing with multiple modules is built in to Maven 2.0. In this section, we will show how to build the WAR above, and include the previous JAR as well in one step.
-
-
03 Apr 12
-
An archetype is defined as an original pattern or model from which all other things of the same kind are made. In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements.
-
packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.
-
${basedir} represents the directory containing pom.xml
-
- mvn test-compile
If you simply want to compile your test sources (but not execute the tests), you can execute the following:
-
- **/*Test.java
- **/Test*.java
- **/*TestCase.java
Note that the surefire plugin (which executes the test) looks for tests contained in files with a particular naming convention. By default the tests included are:
-
The SNAPSHOT value refers to the 'latest' code along a development branch, and provides no guarantee the code is stable or unchanging. Conversely, the code in a 'release' version (any version value without the suffix SNAPSHOT) is unchanging.
In other words, a SNAPSHOT version is the 'development' version before the final 'release' version.
-
The SNAPSHOT is "older" than its release.
-
During the release process, a version of x.y-SNAPSHOT changes to x.y. The release process also increments the development version to x.(y+1)-SNAPSHOT. For example, version 1.0-SNAPSHOT is released as version 1.0, and the new development version is version 1.1-SNAPSHOT.
-
To reference a property defined in your pom.xml, the property name uses the names of the XML elements that define the value, with "pom" being allowed as an alias for the project (root) element. So ${project.name} refers to the name of the project, ${project.version} refers to the version of the project, ${project.build.finalName} refers to the final name of the file created when the built project is packaged, etc. Note that some elements of the POM have default values, so don't need to be explicitly defined in your pom.xml for the values to be available here. Similarly, values in the user's settings.xml can be referenced using property names beginning with "settings" (for example, ${settings.localRepository} refers to the path of the user's local repository).
-
process-resources is the build lifecycle phase where the resources are copied and filtered
-
he scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime.
-
to override this behaviour, you can use the --non-recursive command line option
-
-
carlos puentesThis guide is intended as a reference for those working with Maven for the first time, but is also intended to serve as a cookbook with self-contained references and solutions for common use cases. For first time users, it is recommended that you step through the material in a sequential fashion. For users more familiar with Maven, this guide endeavours to provide a quick solution for the need at hand. It is assumed at this point that you have downloaded Maven and installed Maven on your local machine. If you have not done so please refer to the Download and Installation instructions.
Ok, so you now have Maven installed and we're ready to go. Before we jump into our examples we'll very briefly go over what Maven is and how it can help you with your daily work and collaborative efforts with team members. Maven will, of course, work for small projects, but Maven shines in helping teams operate more effectively by allowing team members to focus on what the stakeholders of a project require. You can leave the build infrastructure to Maven! -
25 Mar 12
-
20 Mar 12
-
Maven project we are going to use Maven's archetype mechanism. An archetype is defined as an original pattern or model from which all other things of the same kind are made. In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements
-
pom.xml contains the Project Object Model (POM) for this project. The POM is the basic unit of work in Maven. This is important to remember because Maven is inherently project-centric in that everything revolves around the notion of a project.
-
-
07 Feb 12
-
05 Feb 12
-
09 Dec 11
Jimmy RoyerThis guide is intended as a reference for those working with Maven for the first time, but is also intended to serve as a cookbook with self-contained references and solutions for common use cases. For first time users, it is recommended that you step through the material in a sequential fashion. For users more familiar with Maven, this guide endeavours to provide a quick solution for the need at hand. It is assumed at this point that you have downloaded Maven and installed Maven on your local machine. If you have not done so please refer to the Download and Installation instructions.
-
11 Oct 11
-
26 Sep 11
-
An archetype is defined as an original pattern or model from which all other things of the same kind are made
-
Whenever you want to customise the build for a Maven project, this is done by adding or reconfiguring plugins.
-
any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR
-
The pom.xml and pom.properties files are packaged up in the JAR so that each artifact produced by Maven is self-describing and also allows you to utilize the metadata in your own application if the need arises
-
Similarly, values in the user's settings.xml can be referenced using property names beginning with "settings" (for example, ${settings.localRepository} refers to the path of the user's local repository).
-
For each external dependency, you'll need to define at least 4 things: groupId, artifactId, version, and scope.
-
The search shows a directory called /maven2/log4j/log4j (or /pub/packages/maven2/log4j/log4j). In that directory is a file called maven-metadata.xml.
-
How do I build more than one project at once?
The concept of dealing with multiple modules is built in to Maven 2.0. In this section, we will show how to build the WAR above, and include the previous JAR as well in one step.
Firstly, we need to add a parent pom.xml file in the directory above the other two, so it should look like this:
-
<modules> <module>my-app</module> <module>my-webapp</module> </modules>
-
Next, we tell the WAR that it requires the my-app JAR. This does a few things: it makes it available on the classpath to any code in the WAR (none in this case), it makes sure the JAR is always built before the WAR, and it indicates to the WAR plugin to include the JAR in its library directory.
-
You may have noticed that junit-3.8.1.jar was a dependency, but didn't end up in the WAR. The reason for this is the <scope>test</scope> element - it is only required for testing, and so is not included in the web application as the compile time dependency my-app is.
-
-
20 Sep 11
-
You see below in our example we have added the directory ${basedir}/src/main/resources into which we place any resources we wish to package in our JAR. The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.
-
pom.properties | `-- pom.xml
-
The pom.xml and pom.properties files are packaged up in the JAR so that each artifact produced by Maven is self-describing and also allows you to utilize the metadata in your own application if the need arises.
-
To reference a property defined in your pom.xml, the property name uses the names of the XML elements that define the value, with "pom" being allowed as an alias for the project (root) element. So ${pom.name} refers to the name of the projec
-
Similarly, values in the user's settings.xml can be referenced using property names beginning with "settings" (for example, ${settings.localRepository} refers to the path of the user's local repository
-
To reference a property defined in an external file, all you need to do is add a reference to this external file in your pom.xml
-
<filters> <filter>src/main/filters/filter.properties</filter> </filters>
-
As an alternative to defining the my.filter.value property in an external file, you could also have defined it in the properties section of your pom.xml and you'd get the same effect
-
<properties> <my.filter.value>hello</my.filter.value> </properties>
-
Filtering resources can also get values from system properties;
-
or properties defined on the command line using the standard Java -D parameter.
-
-
26 Jul 11
-
<filtering>true</filtering>
-
<filter>src/main/filters/filter.properties</filter>
-
-
17 Jul 11
-
06 May 11
-
24 Apr 11
-
07 Mar 11
-
10 Jan 11
-
14 Oct 10
-
27 Aug 10
-
23 Aug 10
-
18 Jul 10
-
07 Jul 10
-
archetype mechanism
-
archetype mechanism
-
An archetype is defined as an original pattern or model from which all other things of the same kind are made. In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user's requirements.
-
POM Reference
-
-
04 Jul 10
-
28 Jun 10
-
07 May 10
-
29 Apr 10
-
23 Feb 10
-
20 Jan 10
-
14 Dec 09
-
30 Oct 09
-
pom.xml contains the Project Object Model (POM) for this project. The POM is the basic unit of work in Maven. This is important to remember because Maven is inherently project-centric in that everything revolves around the notion of a project. In short, the POM contains every important piece of information about your project and is essentially one-stop-shopping for finding anything related to your project
-
where ${basedir} represents the directory containing pom.xml).
-
-
08 Oct 09
-
10 Sep 09
-
31 Aug 09
-
18 Aug 09
-
14 Aug 09
-
13 Aug 09
-
To have Maven filter resources when copying, simply set filtering to true for the resource directory in your pom.xml:
-
You'll notice that we had to add the build, resources, and resource elements which weren't there before. In addition, we had to explicitly state that the resources are located in the src/main/resources directory. All of this information was provided as default values previously, but because the default value for filtering is false, we had to add this to our pom.xml in order to override that default value and set filtering to true.
-
To reference a property defined in your pom.xml, the property name uses the names of the XML elements that define the value, with "pom" being allowed as an alias for the project (root) element. So ${pom.name} refers to the name of the project, ${pom.version} refers to the version of the project, ${pom.build.finalName} refers to the final name of the file created when the built project is packaged, etc. Note that some elements of the POM have default values, so don't need to be explicitly defined in your pom.xml for the values to be available here. Similarly, values in the user's settings.xml can be referenced using property names beginning with "settings" (for example, ${settings.localRepository} refers to the path of the user's local repository).
-
<filters> <filter>src/main/filters/filter.properties</filter> </filters>
-
As an alternative to defining the my.filter.value property in an external file, you could also have defined it in the properties section of your pom.xml and you'd get the same effect
-
Filtering resources can also get values from system properties; either the system properties built into Java (like java.version or user.home) or properties defined on the command line using the standard Java -D parameter
-
-
21 Jul 09
-
23 Jun 09
-
This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plug-ins.
-
he default value for the packaging element is JAR so you do not have to specify this for most projects.
-
This element indicates the display name used for the project. This is often used in Maven's generated documentation
-
mvn compile
-
The first time you execute this (or any other) command, Maven will need to download all the plugins and related dependencies it needs to fulfill the command. From a clean installation of Maven this can take quite a while (in the output above, it took almost 4 minutes). If you execute the command again, Maven will now have what it needs, so it won't need to download anything new and will be able to execute the command much more quickly.
-
As you can see from the output, the compiled classes were placed in ${basedir}/target/classes, which is another standard convention employed by Maven. So, if you're a keen observer, you'll notice that by using the standard conventions the POM above is very small and you haven't had to tell Maven explicitly where any of your sources are or where the output should go
-
mvn test
-
mvn test-compile
-
mvn clean
This will remove the target directory with all the build data before starting so that it is fresh.
-
Perhaps you'd like to generate an IntelliJ IDEA descriptor for the project?
mvn idea:idea
This can be run over the top of a previous IDEA project - it will update the settings rather than starting fresh.
If you are using Eclipse IDE, just call:
mvn eclipse:eclipse
Note: some familiar goals from Maven 1.0 are still there - such as jar:jar, but they might not behave like you'd expect. Presently, jar:jar will not recompile sources - it will simply just create a JAR from the target/classes directory, under the assumption everything else had already been done.
-
including a specific version if you request it (the default is to use the latest available).
-
You see below in our example we have added the directory ${basedir}/src/main/resources into which we place any resources we wish to package in our JAR. The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.
-
s you can see, the contents of ${basedir}/src/main/resources can be found starting at the base of the JAR and our application.properties file is there in the META-INF directory. You will also notice some other files there like META-INF/MANIFEST.MF as well as a pom.xml and pom.properties file. These come standard with generation of a JAR in Maven. You can create your own manifest if you choose, but Maven will generate one by default if you don't. (You can also modify the entries in the default manifest. We will touch on this later.) The pom.xml and pom.properties files are packaged up in the JAR so that each artifact produced by Maven is self-describing and also allows you to utilize the metadata in your own application if the need arises. One simple use might be to retrieve the version of your application. Operating on the POM file would require you to use some Maven utilities but the properties can be utilized using the standard Java API and look like the following:
-
To add resources to the classpath for your unit tests, you follow the same pattern as you do for adding resources to the JAR except the directory you place resources in is ${basedir}/src/test/resources. At this point you would have a project directory structure that would look like the following:
-
// Retrieve resource InputStream is = getClass().getResourceAsStream( "/test.properties" ); // Do something with the resource ...
-
Filtering resources can also get values from system properties; either the system properties built into Java (like java.version or user.home) or properties defined on the command line using the standard Java -D parameter. To continue the example, let's change our application.properties file to look like this:
-
Now, when you execute the following command (note the definition of the command.line.prop property on the command line), the application.properties file will contain the values from the system properties.
-
The dependencies section of the pom.xml lists all of the external dependencies that our project needs in order to build (whether it needs that dependency at compile time, test time, run time, or whatever). Right now, our project is depending on JUnit only (I took out all of the resource filtering stuff for clarity):
-
he scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime.
-
y, the parent POM created (called app), has a packaging of pom and a list of modules defined. This tells Maven to run all operations over the set of projects instead of just the current one
-
Unlike Maven 1.0, it is not required that you run install to successfully perform these steps - you can run package on its own and the artifacts in the reactor will be used from the target directories instead of the local repository.
-
-
06 Jun 09
-
15 May 09
-
07 Apr 09
-
22 Mar 09
-
11 Mar 09
-
13 Nov 08
-
05 Oct 08
-
03 Oct 08
-
05 Jun 08
-
09 May 08
-
02 Feb 08
-
23 Jan 08
-
Maven shines in helping teams operate more effectively by allowing team members to focus on what the stakeholders of a project require. You can leave the build infrastructure to Maven!
-
Maven is essentially a project management and comprehension tool and as such provides a way to help with managing
-
-
11 Nov 07
-
01 Oct 07
-
30 Sep 07
-
28 Aug 07
egidio brigentie the cache location or are behind a HTTP proxy, you will need to create configuration. See the Guide to Configuring Maven for more information.
-
25 Jun 07
-
27 Apr 07
-
26 Apr 07
-
22 Feb 07
-
12 Oct 06
-
28 Sep 06
-
09 Sep 06
-
03 Sep 06
-
16 Apr 06
-
03 Apr 06
-
16 Mar 06
-
This guide is intended as a reference for those working with Maven for the first time, but is also intended to serve as a cookbook with self-contained references and solutions for common usecases.
-
-
25 Feb 06
-
04 Jan 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.