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.
C:\Users\<username>\.m2
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 |
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 |
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
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
${project.basedir} | main project directory |
${project.parent.basedir} | parent pom's directory |
${project.parent.parent.basedir} | grand parent pom's directory |
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/
To avoid this error during mvn clean install assign more space to Java. Use an environment variable MAVEN_OPTS
set MAVEN_OPTS=-Xmx1024m
mvn dependency:tree -Dincludes="this.is.your.gorup.id:this.is.the.artifact.id"
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
Example:
target: 1.0-SNAPSHOT deploy: 1.0-20080207-230803-1
Frther there is an OSGI Version too. It is called qualifier. It is replaced by a timestamp in maven plugins.
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. |
Syntax to use the profile jetty
mvn clean install -P jetty
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>
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> ...