maven basic concept
DESCRIPTION
Maven basic conceptTRANSCRIPT
Maven
Outline
• POM
• Dependency
• Life cycle
• Plugin
• Profile
• SCM
Maven
• Dependency Management
• Remote Repositories
• Universal Reuse of Build Logic
• Tool Portablility/Integration
• Easy Searching and Filtering of Project Artifact
Project Object Model (POM)
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.estinet</groupId>
<artifactId>controller</artifactId>
<version>1.0.0</version>
</project>
•groupId•Group identifier•Java package name
•arti•project main identifier
Effective POM
• mvn help:effective-pom• de
Super POM
com.estinet.controllerbod
1.0-SNAPSHOT
com.estinet.switchbroadcom
1.0-SNAPSHOT
Project Inheritance
• dependencies
• developers and contributors
• plugin lists (including reports)
• plugin executions with matching ids
• plugin configuration
• resources
Project Inhertance
<project>
<parent>
<groupId>com.estinet</groupId>
<artifactId>controller</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>bod</artifactId>
</project>
SNAPSHOT Version
• Expand SNAPSHOT to UTC date and time
• e.g.,– 1.0-SNAPSHOT– 1.0-20160616-150000-1
Properties• env
– exposed by OS
${evn.PATH}
• project– project variable, defined in POM
${project.groupId}
• setting– setting variable– ./.m2/setting.xml
${setting.offline}
Project Dependency
<project>
...
<dependencies>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-java5</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
...
</dependency>
</dependencies>
</project>
Dependency Scope
• compile– default
• provided– required on compilation, but not for execution– provided by JDK or other container
• runtime– required for execution and test
• test– for test only
• system– like provided– must supply systemPath
Grouping Dependencies<project>
<groupId>com.estinet.controller</groupId>
<artifactId>controller-dependencies</artifactId>
<version>1.0.0</version>
<packaging>POM<packaging>
<dependencies>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-java5</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
...
</dependency>
</dependencies>
</project>
install POM to your local repository (~/.m2) mvn install
Grouping Dependencies
<project>
...
<dependencies>
<dependency>
<groupId>com.estinet.controller</groupId>
<artifactId>controller-dependencies</artifactId>
<version>1.0.0</version>
<type>POM</type>
</dependency>
<dependency>
...
</dependency>
</dependencies>
</project>
Multi-Modules
<project>
...
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
</project>
Maven Usages
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Lifecycle
lifecycle description
clean clean the build directory
default complie, test, package, deploy
site report, document
Clean Lifecycle
Phase
pre-clean
clean
post-clean
command description
mvn clean execute the clean lifecycleincluding pre-clean, clean, post-clean
mvn clean:clean execute the clean phase of the clean lifecycle
Default LifecyclePhase Phase
validate test
generate-sources prepare-package
process-sources package
generate-resources pre-integration-test
process-resources verify
compile install
generate-test-sources deploy
process-test-sources
generate-test-resources
process-test-resources
test-compile
Default Lifecycle
command description
mvn validate execute validate phase
mvn compile execute from validate phase to compile phase
mvn test execute from validate phase to test phase
mvn install execute fomr validate phase to install phase
Package-specific Lifecycle (JAR)
Lifecycle Phase Goal <pluging>:<goal>
process-resource resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
install install:install
deploy deploy:deploy
Package-specific Lifecycle (POM)
Lifecycle Phase Goal <pluging>:<goal>
package site:attach-descriptor
install install:install
deploy deploy:deploy
Test• Run as JUNIT test• Variables
– testSourceDirectory• src/test/java
– testOutputdirectory• target/test-classes
– result• target/surefire-reports
• Includsion– Java filenames that start with "Test"
• **/Test*.java– Java filenames that end with "Test"
• **/*Test.java– Java filenames that end with "TestCase"
• **/*TestCase.java
Test
mvn -Dtest=TestCircle test
run test on TestCircle class
mvn -Dtest=TestSquare,TestCi*le test
run multiple names/patterns, separated by commas
mvn -Dtest=TestCircle#mytest testmvn -Dtest=TestCircle#test* testmvn -Dtest=TestCircle#testOne+testTwo test
run test on multiple methods
mvn install -Dmaven.test.skip=true
skip test
Package-specific Lifecycle (Maven Plugin)
Lifecycle Phase Goal <pluging>:<goal>
generate-resource plugin:descriptor
process-resources resources:resources
compile compiler:compile
process-test-resource resources:testResources
test-compile compile:testCompile
test surefire:test
package jar:jar, plugin:addPluginArtifactMetaData
deploy deploy:deploy
Process Resource
copy from
${basedir}/src/main/resource
to
${project.build.outputDirector}
(${basedir}/target/classes by default)
Compile
<project>
...
<build>
...
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</aftifactId>
<configuration>
<source>1.6</source>
<target>1.5</tartet>
</configuratin>
</plugin>
</plugins>
</build>
</project>
source java 1.6vm java 1.5
Compile
<project>
...
<build>
...
<sourceDirectory>src/java</sourceDirectory>
<outputDirectory>classes</outputDirectory>
</build>
</project>
Plugin
<project> ... <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </pluginManagement> </build></project>
Configure variable used by plugin
"Maven" is really just a core framework for a collection of Maven Plugins
<project> ... <build> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.2</version> <configuration> <doCheck>false</doCheck> <doUpdate>false</doUpdate> <revisionOnScmFailure>VersionUnknown</revisionOnScmFailure> </configuration> <executions> <execution> <goals> <goal>create</goal> </goals> <phase>validate</phase> </execution> </executions> </plugin> </plugins> </build></project>
execute the plugin goal create in the validate phase
<project> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> ...... </configuration> </execution> </executions> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> </plugin> </plugins> </build></project>
use pluginManagement to management plugin configuration
Profile
• Per Project– pom.xml
• Per User– %USER_HOME%/.m2/settings.xml
• Global– %M2_HOME%/config/settings.xml
mvn groupId:artifactId:goal -P profile-1,profile-2
Profile for Development
<profile> <id>development</id> <properties> <db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass> <db.connectionURL>jdbc:oracle:thin:@127.0.0.1:1521:XE</db.connectionURL> <db.username>devuser</db.username> <db.password>devpassword</db.password> <logo.image>mylogo.png</logo.image> </properties></profile>
mvn groupId:artifactId:goal -P development
Profile for Production<profile> <id>production</id> <properties> <db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass> <db.connectionURL>jdbc:oracle:thin:@10.0.1.14:1521:APPS</db.connectionURL> <db.username>productionuser</db.username> <db.password>productionpassword</db.password> <logo.image>production_logo.png</logo.image> </properties></profile>
mvn groupId:artifactId:goal -P production
Resource Filtering
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource></resources>
Enable resource filtering in specific directorymaven variable in files in the specific directory will be replaced
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.3</version> <configuration> <encoding>UTF-8</encoding> </configuration></plugin>
add plugin
configure resource filtering
Resource Filtering
database.jdbc.driverClass=${db.driverClass}database.jdbc.connectionURL=${db.connectionURL}database.jdbc.username=${db.username}database.jdbc.password=${db.password}
model.application.name=MyWebAppmodel.stylesheet=/mywebapp.css
database.jdbc.driverClass=oracle.jdbc.driver.OracleDriverdatabase.jdbc.connectionURL=jdbc:oracle:thin:@127.0.0.1:1521:XEdatabase.jdbc.username=myusernamedatabase.jdbc.password=mypassword
model.application.name=MyWebAppmodel.stylesheet=/mywebapp.css
db.properties source
db.properties source after resource filtering
Profile Activation
<profiles> <profile> <id>appserverConfig-dev</id> <activation> <property> <name>env</name> <value>dev</value> </property> </activation> <properties> <server.home>/appserver</server.home> </properties> </profile><profiles>
<profiles> <profile> <id>appserverConfig-dev-2</id> <activation> <property> <name>env</name> <value>dev-2</value> </property> </activation> <properties> <server.home>/appserver2</server.home> </properties> </profile><profiles>
actived if env = dev actived if env = dev2
Source Code Management (SCM)
Use SCM to update and checkin code<project> ... <scm> <connection>scm:svn:http://somerepository.com/svn_repo/trunk</connection> <developerConnection>scm:svn:https://somerepository.com/svn_repo/trunk</developerConnection> <url>http://somerepository.com/view.cvs</url> </scm> ...</project>
mvn -Dmessage="<commit_log_here>" scm:checkin
mvn scm:update
check in code
update
Name Value
-Dmaven.test.skip=true skip test
-o work offline, skip download pom
-P <args> configurate active profile
-X enable debug output
-q quiet output, only show errors
-npu no pluging updates
help:active-profiles
help:effective-pom output effective pom
help:effective-setting output effective setting
help:describe describe plugin, mvn help:describe -Dplugin=help