Your SlideShare is downloading. ×
0
Apache Maven
Volodymyr Ostapiv
2014
Agenda
▪ What is Maven
▪ Getting and installing
▪ Basics: POM, GAV, Archetype
▪ pom.xml contents
▪ Some useful plugins
What is Maven
▪Maven is a build automation tool used
primarily for Java projects.
▪ Maven addresses two aspects of buildin...
Maven LifeCycle
•Default lifecycle
–generate-sources/generate-resources
–compile
–test
–package
–integration-test (pre and...
History
– Maven 1 (2003)
– Maven 2 (2005)
▪ Not backwards Compatible
– Maven 3 (2010)
▪ Same as Maven 2 but more stable an...
Get it!
Configure
%MAVEN_HOME%confsettings.xml
/usr/local/maven/conf/settings.xml
UserProfile.m2
~/.m2/
Create simple project
mvn archetype:generate
-DgroupId=[myGroup]
-DartifactId=[myArtifact]
-DarchetypeArtifactId=maven-
ar...
Arche-who? O_o
An archetype is defined as an original pattern or model
from which all other things of the same kind are ma...
Maven + IDE
mvn idea:idea mvn eclipse:eclipse
Generate project files for the most popular IDEs
Path Conventions
Maven Project Object Model (POM)
▪ Describes a project
– Name and Version
– Artifact Type
– Source Code Locations
– Depend...
Project Name (GAV)
▪ Maven uniquely identifies a project using:
– groupID: project grouping identifier (no spaces
or colon...
Project Name (GAV)
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.lds.tr...
Dependencies
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5...
Dependency scope
• compile (default)
• provided (by JDK or a container at runtime)
• runtime (not required for compilation...
Properties
<properties>
<jdk.version>1.6</jdk.version>
<spring.version>3.1.2.RELEASE</spring.version>
<spring.batch.versio...
Exclusions
<exclusions>
<exclusion>
<groupId>org.softserve.sse</groupId>
<artifactId>softserve-sse</artifactId>
</exclusio...
Profiles
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
(mvn ins...
Profile activation
Operating System based:
<activation>
<os>
<family>unix</family>
</os>
</activation>
<activation>
<os>
<...
Profile activation
System property based:
<activation>
<property>
<name>environment</name>
<value>test</value>
</property>...
Plugins
Here real magic starts
maven-surefire-plugin
surefire:test
jetty-maven-plugin
mvn jetty:run
tomcat-maven-plugin
mvn tomcat:run
maven-antrun-plugin
mvn antrun:run
maven-compiler-plugin
mvn compiler:compile
mvn compiler:testCompile
maven-assembly-plugin
mvn assembly:single
maven-shade-plugin
mvn shade:shade
maven-dependency-plugin
mvn dependency:analyze
mvn dependency:tree
That’s all
Upcoming SlideShare
Loading in...5
×

Apache Maven for SoftServe IT Academy

2,144

Published on

Presentation about Apache Maven for SoftServe IT Academy
http://akceptor.org/yak-ya-potrapyv-u-it-akademiyu-softserve/

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,144
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • http://maven.apache.org/
  • Build automation is the act of scripting or automating a wide variety of tasks that software developers do in their day-to-day activities including things like:compiling computer source code into binary codepackaging binary coderunning testsdeployment to production systemscreating documentation and/or release notes
  • mvn installInvokes generate* and compile, test, package, integration-test, installmvn clean Invokes just cleanmvn clean compileClean old builds and execute generate*, compilemvn compile installInvokes generate*, compile, test, integration-test, package, installmvn test cleanInvokes generate*, compile, test then cleans
  • 1. Install JDK, set JAVA_HOME2. Download and extract Apache Maven (http://maven.apache.org/)3. Add MAVEN_HOME system variable4. Add MAVEN_HOME to PATH5.“mvn –version“.
  • &lt;project&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;groupId&gt;com.softserve.demo&lt;/groupId&gt; &lt;artifactId&gt;simple-web-project&lt;/artifactId&gt; &lt;packaging&gt;war&lt;/packaging&gt; &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;junit&lt;/groupId&gt; &lt;artifactId&gt;junit&lt;/artifactId&gt; &lt;version&gt;3.8.1&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;build&gt; &lt;finalName&gt;simple-web-project&lt;/finalName&gt; &lt;/build&gt; &lt;/project&gt;
  • mvnarchetype:generate-DgroupId={com.softserve.demo}-DartifactId={simple-web-project}-DarchetypeArtifactId=maven-archetype-webapp-DinteractiveMode=false
  • Maven is opinionated about project structuretarget: Default work directorysrc: All project source files go in this directorysrc/main: All sources that go into primary artifactsrc/test: All sources contributing to testing projectsrc/main/java: All java source filessrc/main/webapp: All web source filessrc/main/resources: All non compiled source filessrc/test/java: All java test source filessrc/test/resources: All non compiled test source files
  • &lt;dependency&gt; &lt;groupId&gt;net.lightbody.bmp&lt;/groupId&gt; &lt;artifactId&gt;browsermob-proxy&lt;/artifactId&gt; &lt;version&gt;2.0-beta-8&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt;&lt;exclusions&gt;&lt;exclusion&gt; &lt;groupId&gt;org.softserve.sse&lt;/groupId&gt; &lt;artifactId&gt;softserve-sse&lt;/artifactId&gt; &lt;/exclusion&gt; &lt;/exclusions&gt;&lt;/dependency&gt;
  • &lt;profiles&gt;&lt;profile&gt;&lt;id&gt;default&lt;/id&gt;&lt;activation&gt; &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;&lt;/activation&gt; &lt;build&gt; &lt;finalName&gt;${name}&lt;/finalName&gt; &lt;plugins&gt; &lt;plugin&gt;…&lt;/plugin&gt; &lt;plugins&gt; &lt;/build&gt;&lt;/profile&gt;&lt;/profiles&gt;
  • &lt;activation&gt; &lt;os&gt; &lt;family&gt;windows&lt;/family&gt; &lt;/os&gt;&lt;/activation&gt;
  • Use –DskipTests to skip test
  • http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
  • http://tomcat.apache.org/maven-plugin.html
  • https://maven.apache.org/plugins/maven-antrun-plugin/
  • &lt;plugin&gt; &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &lt;version&gt;2.3.1&lt;/version&gt; &lt;configuration&gt; &lt;source&gt;1.6&lt;/source&gt; &lt;target&gt;1.6&lt;/target&gt; &lt;compilerArgument&gt;-proc:none&lt;/compilerArgument&gt; &lt;/configuration&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;run-annotation-processors-only&lt;/id&gt; &lt;phase&gt;generate-sources&lt;/phase&gt; &lt;configuration&gt; &lt;compilerArgument&gt;-proc:only&lt;/compilerArgument&gt; &lt;!-- If your app has multiple packages, use this include filter to execute the processor only on the package containing your entities --&gt; &lt;!-- &lt;includes&gt; &lt;include&gt;**/model/*.java&lt;/include&gt; &lt;/includes&gt; --&gt; &lt;/configuration&gt; &lt;goals&gt; &lt;goal&gt;compile&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; &lt;/executions&gt;&lt;/plugin&gt;
  • For Spring apps use onejar-maven-plugin&lt;plugin&gt;&lt;groupId&gt;com.jolira&lt;/groupId&gt;&lt;artifactId&gt;onejar-maven-plugin&lt;/artifactId&gt;&lt;version&gt;1.4.4&lt;/version&gt;&lt;executions&gt;&lt;execution&gt;&lt;configuration&gt;&lt;mainClass&gt;com.package.core.Main&lt;/mainClass&gt;&lt;!-- Optional, default is false --&gt;&lt;attachToBuild&gt;true&lt;/attachToBuild&gt;&lt;!-- Optional, default is &quot;one jar&quot; --&gt;&lt;classifier&gt;onejar&lt;/classifier&gt;&lt;/configuration&gt;&lt;goals&gt;&lt;goal&gt;one-jar&lt;/goal&gt;&lt;/goals&gt;&lt;/execution&gt;&lt;/executions&gt;&lt;/plugin&gt;
  • http://maven.apache.org/plugins/maven-shade-plugin/
  • &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt; &lt;version&gt;2.4&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;analyze&lt;/id&gt; &lt;phase&gt;verify&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;analyze-only&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;failOnWarning&gt;true&lt;/failOnWarning&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt;&lt;/plugin&gt;
  • Transcript of "Apache Maven for SoftServe IT Academy"

    1. 1. Apache Maven Volodymyr Ostapiv 2014
    2. 2. Agenda ▪ What is Maven ▪ Getting and installing ▪ Basics: POM, GAV, Archetype ▪ pom.xml contents ▪ Some useful plugins
    3. 3. What is Maven ▪Maven is a build automation tool used primarily for Java projects. ▪ Maven addresses two aspects of building software: 1. it describes how software is built 2. it describes its dependencies
    4. 4. Maven LifeCycle •Default lifecycle –generate-sources/generate-resources –compile –test –package –integration-test (pre and post) –install –Deploy •Separate “clean” lifecycle
    5. 5. History – Maven 1 (2003) – Maven 2 (2005) ▪ Not backwards Compatible – Maven 3 (2010) ▪ Same as Maven 2 but more stable and with some additional features
    6. 6. Get it!
    7. 7. Configure %MAVEN_HOME%confsettings.xml /usr/local/maven/conf/settings.xml UserProfile.m2 ~/.m2/
    8. 8. Create simple project mvn archetype:generate -DgroupId=[myGroup] -DartifactId=[myArtifact] -DarchetypeArtifactId=maven- archetype-archetype OR mvn archetype:generate
    9. 9. Arche-who? O_o An archetype is defined as an original pattern or model from which all other things of the same kind are made. Archetype is a Maven project templating toolkit.
    10. 10. Maven + IDE mvn idea:idea mvn eclipse:eclipse Generate project files for the most popular IDEs
    11. 11. Path Conventions
    12. 12. Maven Project Object Model (POM) ▪ Describes a project – Name and Version – Artifact Type – Source Code Locations – Dependencies – Plugins – Profiles (Alternate build config.) ▪ Uses XML by default
    13. 13. Project Name (GAV) ▪ Maven uniquely identifies a project using: – groupID: project grouping identifier (no spaces or colons) ▪ Usually loosely based on Java package – artfiactId: name of project (no spaces or colons) – version: Version of project ▪ Format {Major}.{Minor}.{Maintanence} ▪ Add ‘-SNAPSHOT ‘ to identify in development
    14. 14. Project Name (GAV) <?xml version="1.0" encoding="UTF-8"?> <project> <modelVersion>4.0.0</modelVersion> <groupId>org.lds.training</groupId> <artifactId>maven-training</artifactId> <version>1.0</version> <name>Windows 8</name> <description>The best OS ever!</description> <packaging>jar</packaging> <properties> <java.version>1.6</java.version> <properties> </project>
    15. 15. Dependencies <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.5</version> <scope>compile</scope> </dependency> <dependencies>
    16. 16. Dependency scope • compile (default) • provided (by JDK or a container at runtime) • runtime (not required for compilation) • test (used only during tests) • system • import (only available in Maven 2.0.9 or later)
    17. 17. Properties <properties> <jdk.version>1.6</jdk.version> <spring.version>3.1.2.RELEASE</spring.version> <spring.batch.version>2.1.8.RELEASE</spring.batch.version> </properties> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-infrastructure</artifactId> <version>${spring.batch.version}</version> </dependency> mvn install -Dmyproperty=my property from command line
    18. 18. Exclusions <exclusions> <exclusion> <groupId>org.softserve.sse</groupId> <artifactId>softserve-sse</artifactId> </exclusion> </exclusions>
    19. 19. Profiles <profile> <id>default</id> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> (mvn install -Pprofilename)
    20. 20. Profile activation Operating System based: <activation> <os> <family>unix</family> </os> </activation> <activation> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> </activation> JDK based: <activation> <jdk>1.4</jdk> </activation> <activation> <jdk>[1.3,1.6)</jdk> </activation>
    21. 21. Profile activation System property based: <activation> <property> <name>environment</name> <value>test</value> </property> </activation> File system state based: <activation> <file> <missing> target/maven/somefile </missing> </file> </activation>
    22. 22. Plugins Here real magic starts
    23. 23. maven-surefire-plugin surefire:test
    24. 24. jetty-maven-plugin mvn jetty:run
    25. 25. tomcat-maven-plugin mvn tomcat:run
    26. 26. maven-antrun-plugin mvn antrun:run
    27. 27. maven-compiler-plugin mvn compiler:compile mvn compiler:testCompile
    28. 28. maven-assembly-plugin mvn assembly:single
    29. 29. maven-shade-plugin mvn shade:shade
    30. 30. maven-dependency-plugin mvn dependency:analyze mvn dependency:tree
    31. 31. That’s all
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×