Table of Contents
Maven
Maven is an project automation tool. All the routine work can be automated by one maven script: e.g. compiling, project1 and project2, generating websites, run the tests.
- Maven can manage Maven-artifacts, which are uniquely identified by: groupId, artifactId, version.
- Maven artifacts can depend on other Maven artifacts.
- Maven downloads required artifacts, which the local artifacts depend from - all into one local repository:
C:\Users\<username>\.m2
- Maven has an own Project type in Eclipse. Each Maven Project is a Maven artifact.
What | Where |
---|---|
1. General “what is maven” description. | What is maven? |
2. Project requred fileds: groupId, artifactId, version | Requred fields |
3. Maven Phases, and so the commands | Maven phases |
4. Further details about Maven | maven.apache.org |
Maven
Useful maven resources
Glossary
Lifecycle | There is a predefined lifecycle. Maven executed all phases of this lifecycle http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings |
Phase | Generate-sources, compile, test-compile,…, install, deploy |
Goal | Goals are executed in phases. (test, jar, install). Goals are defined by plugins. Each executed plugin binds it's goals to predefined phases. |
Mojo | Maven Plain Old Java Object. Each Mojo is a goal in maven |
Phases
- install - installiert in Lokale Repo
- deploy - installiert ins Nexus
Goals and Phases
The Syntax to execute a phase
mvn phasename
Examples:
mvn clean mvn deploy mvn install
Achtung: Executing a phase - executes all phases up to the executed phase!
Executing:
mvn clean
does
process-resources compile process-test-resources test-compile test package install
The Syntax to execute a goal
mvn myplugin:myGoal
Examples:
mvn jboss-as:deploy mvn dependency:copy-dependencies
You can mix up executing phases (e.g. clean or package) with goal-execution of some plugins (e.g. dependency:copy-dependencies)
Examples:
mvn clean dependency:copy-dependencies package
Variables in pom.xml
${project.basedir} | main project directory |
${project.parent.basedir} | parent pom's directory |
${project.parent.parent.basedir} | grand parent pom's directory |
Usage
Merging
Child POMs override parent Pom's nodes on default.
How to change this behaviour is described here: http://blog.sonatype.com/people/2011/01/maven-how-to-merging-plugin-configuration-in-complex-projects/
Settings
java.lang.OutOfMemoryError
To avoid this error during mvn clean install assign more space to Java. Use an environment variable MAVEN_OPTS
set MAVEN_OPTS=-Xmx1024m
Abhängigkeiten visualisieren
Visualize on Console
mvn dependency:tree -Dincludes="this.is.your.gorup.id:this.is.the.artifact.id"
Visualize as PNG
mvn org.fusesource.mvnplugins:maven-graph-plugin:RELEASE:reactor
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <profiles> <profile> <repositories> <repository> <id>maven2-central-remote</id> <name>maven2-central-remote</name> <url>https://mydomain.com/artifactory/maven2-central-remote</url> </repository> <repository> <id>maven2-priv</id> <name>maven2-priv</name> <url>https://mydomain.com/artifactory/maven2-priv</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>maven2-central-remote</id> <url>https://mydomain.com/artifactory/maven2-central-remote</url> </pluginRepository> <pluginRepository> <id>center</id> <url>https://repo1.maven.org/maven2/</url> </pluginRepository> <pluginRepository> <id>jcenter</id> <url>https://jcenter.bintray.com</url> </pluginRepository> </pluginRepositories> <id>bt</id> </profile> </profiles> <activeProfiles> <activeProfile>bt</activeProfile> </activeProfiles> <proxies> <proxy> <active>true</active> <host>111.122.3.44</host> <port>9400</port> <nonProxyHosts>mydomain.com|*.mydomain.com</nonProxyHosts> </proxy> </proxies> <servers> <server> <id>maven2-priv</id> <username>username@domain.com</username> <!-- encrypted pass or api token from artifactory profile --> <password>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</password> </server> <server> <id>maven2-priv-snap</id> <username>username@domain.com</username> <!-- encrypted pass or api token from artifactory profile --> <password>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</password> <!-- encrypted pass --> </server> </servers> </settings>
Now you can add the following to your projects pom.xml
<project .... <pluginRepositories> <pluginRepository> <id>maven2-priv</id> <url>https://domain.com/artifactory/maven2-priv/</url> </pluginRepository> </pluginRepositories> <distributionManagement> <repository> <uniqueVersion>false</uniqueVersion> <id>maven2-priv</id> <name>maven2-priv</name> <url>https://domain.com/artifactory/maven2-priv</url> <layout>default</layout> </repository> <snapshotRepository> <uniqueVersion>false</uniqueVersion> <id>maven2-priv-snap</id> <name>maven2-priv</name> <url>https://domain.com/artifactory/maven2-priv</url> <layout>default</layout> </snapshotRepository> </distributionManagement> ... </project>
# The following command deploys the artifact to the Artifactory.
mvn clean install mvn clean deploy
Version Vodoo
SNAPSHOT
- The Word SNAPSHOT is interpreted by Maven, if it is contained inside the version string.
- The Word SNAPSHOT is interpreted during install or deploy phases. Inside the target folder the filename will still contain SNAPSHOT. After deploy it will be replaced.
- The Word SNAPSHOT is expanded to a qualifier, e.g. to date and time.
Example:
target: 1.0-SNAPSHOT deploy: 1.0-20080207-230803-1
qualifier
Frther there is an OSGI Version too. It is called qualifier. It is replaced by a timestamp in maven plugins.
Dependency Management and Scope
Maven manages the dependencies transitively. Here are the rules, about how maven resolves dependencies: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
Scope is a concept, which allows to say, that dependency X is only available at runtime, or should not be inherited transitively.
Scopes:
compile | This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects. |
provided | This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive. |
runtime | This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath. |
test | This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. |
system | This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository. |
import | (only available in Maven 2.0.9 or later)This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency. |
Profiles
Syntax to use the profile jetty
mvn clean install -P jetty
Plugins
If you keep the a part of the xml-structure and insert the structure between the <profile></profile> tags.
Define some plugins which will only be used when profile jetty is enabled
<!-- profile to start jetty --> <profile> <id>jetty</id> <build> <plugins> <plugin> ... </plugin> </plugins> </build> </profile>
Properties
You can use properties to configure the usual build
<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> <artifactId>de.updatesite.customplugins.site</artifactId> <name>de.updatesite</name> <description>updatesite</description> <packaging>${package.method}</packaging> <!-- profile to start jetty will be packaged as pom --> <profile> <properties> <package.method>pom</package.method> </properties> </profile> ...