maven
TRANSCRIPT
What is Maven?
Maven v/s ANT
Create a simple project in Maven
mvn archetype:generate -DgroupId=com.lumesse.talentcloud -DartifactId=sample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Maven Build Lifecycle and Phases:
Built-in Build Lifecycles:
• Default, Clean and Site• The default lifecycle handles your project deployment, the clean lifecycle handles project
cleaning, while the site lifecycle handles the creation of your project's site documentation.
Standard Lifecycles:
• 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.• integration-test: process and deploy the package if necessary into an environment where
integration tests can be run• verify: run any checks to verify the package is valid and meets quality criteria• install: install the package into the local repository, for use as a dependency in other projects
locally• deploy: done in an integration or release environment, copies the final package to the remote
repository for sharing with other developers and projects.
Configuring Plug-ins
• Build plugins will be executed during the build and then, they should be configured in the <build/> element.
• Reporting plugins will be executed during the site generation and they should be configured in the <reporting/> element.
...<build> <plugins> <plugin> <groupId>org.codehaus.modello</groupId> <artifactId>modello-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <configuration> <models> <model>src/main/mdo/maven.mdo</model> </models> <version>4.0.0</version> </configuration>
<phase>generate-sources</phase> <goals> <goal>java</goal> </goals> </execution> </executions> </plugin></plugins> </build>...
package sample.plugin;
import org.apache.maven.plugin.AbstractMojo;import org.apache.maven.plugin.MojoExecutionException;import org.apache.maven.plugins.annotations.Mojo;
/** * Says "Hi" to the user. * */@Mojo( name = "sayhi")public class GreetingMojo extends AbstractMojo{ public void execute() throws MojoExecutionException { getLog().info( "Hello, world." ); }}
Simple MOJO Example
<project> <modelVersion>4.0.0</modelVersion>
<groupId>sample.plugin</groupId> <artifactId>hello-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <packaging>maven-plugin</packaging>
<dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0</version> </dependency> </dependencies></project>
MOJO’s pom.xml
... <build> <plugins> <plugin> <groupId>sample.plugin</groupId> <artifactId>hello-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> </plugin> </plugins> </build>...
Executing Your First Mojo
mvn sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi
mvn hello:sayhi
/** * The greeting to display. */ @Parameter( property = "sayhi.greeting", defaultValue = "Hello World!" ) private String greeting;
Defining Parameters Within a Mojo
Configuring Parameters in a Project
<plugin> <groupId>sample.plugin</groupId> <artifactId>hello-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <greeting>Welcome</greeting> </configuration></plugin>
Using Executions Tag<project> ... <build> <plugins> <plugin> <artifactId>maven-myquery-plugin</artifactId> <version>1.0</version> <executions> <execution> <id>execution1</id> <phase>test</phase> <configuration> <url>http://www.foo.com/query</url> <timeout>10</timeout> <options> <option>one</option> <option>two</option> <option>three</option> </options> </configuration> <goals> <goal>query</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ...</project>
<executions> <execution> <id>execution1</id> <phase>test</phase> <configuration> <url>http://www.foo.com/query</url> <timeout>10</timeout> <options> <option>one</option> <option>two</option> <option>three</option> </options> </configuration> <goals> <goal>query</goal> </goals> </execution> <execution> <id>execution2</id> <configuration> <url>http://www.bar.com/query</url> <timeout>15</timeout> <options> <option>four</option> <option>five</option> <option>six</option> </options> </configuration> <goals> <goal>query</goal> </goals> </execution> </executions>
Maven Antrun Plug-in<project> [...] <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <phase> <!-- a lifecycle phase --> </phase> <configuration> <target> <!-- Place any Ant task here. You can add anything you can add between <target> and </target> in a build.xml. --> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> [...]</project>
Dependency Scopes – System <project> ... <dependencies> <dependency> <groupId>javax.sql</groupId> <artifactId>jdbc-stdext</artifactId> <version>2.0</version> <scope>system</scope> <systemPath>${java.home}/lib/rt.jar</systemPath> </dependency> </dependencies> ...</project>
Good to Know Facts1) Surefire plugin:
Accepted class names are **/*Test.java **/Test*.java **/*TestCase.java And the default excludes are: **/Abstract*Test.java **/Abstract*TestCase.java
2) Putting a jar in local repositorymvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -DgeneratePom=true
Good to Know Facts3) Skip tests in Maven:
-Dmaven.test.skip=true skips test compilation and execution-DskipTests=true skips test execution only
4) Produce execution debug output or error messages in MavenUse -X parameter or -e parameter
References
• http://maven.apache.org/index.html
• http://maven.apache.org/plugins/index.html
• http://maven.apache.org/developers/index.html
Creating your own Archetypes• An archetype descriptor (archetype.xml in directory:
src/main/resources/META-INF/maven• The prototype files that are copied by the archetype plugin (directory:
src/main/resources/archetype-resources/)• The prototype pom (pom.xml in: src/main/resources/archetype-
resources)• A pom for the archetype (pom.xml in the archetype's root directory).