Apache Maven for SoftServe IT Academy

2,384
-1

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,384
On Slideshare
0
From Embeds
0
Number of Embeds
7
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“.
  • <project> <modelVersion>4.0.0</modelVersion> <groupId>com.softserve.demo</groupId> <artifactId>simple-web-project</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>simple-web-project</finalName> </build> </project>
  • 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
  • <dependency> <groupId>net.lightbody.bmp</groupId> <artifactId>browsermob-proxy</artifactId> <version>2.0-beta-8</version> <scope>test</scope><exclusions><exclusion> <groupId>org.softserve.sse</groupId> <artifactId>softserve-sse</artifactId> </exclusion> </exclusions></dependency>
  • <profiles><profile><id>default</id><activation> <activeByDefault>true</activeByDefault></activation> <build> <finalName>${name}</finalName> <plugins> <plugin>…</plugin> <plugins> </build></profile></profiles>
  • <activation> <os> <family>windows</family> </os></activation>
  • 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/
  • <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArgument>-proc:none</compilerArgument> </configuration> <executions> <execution> <id>run-annotation-processors-only</id> <phase>generate-sources</phase> <configuration> <compilerArgument>-proc:only</compilerArgument> <!-- If your app has multiple packages, use this include filter to execute the processor only on the package containing your entities --> <!-- <includes> <include>**/model/*.java</include> </includes> --> </configuration> <goals> <goal>compile</goal> </goals> </execution> </executions></plugin>
  • For Spring apps use onejar-maven-plugin<plugin><groupId>com.jolira</groupId><artifactId>onejar-maven-plugin</artifactId><version>1.4.4</version><executions><execution><configuration><mainClass>com.package.core.Main</mainClass><!-- Optional, default is false --><attachToBuild>true</attachToBuild><!-- Optional, default is "one jar" --><classifier>onejar</classifier></configuration><goals><goal>one-jar</goal></goals></execution></executions></plugin>
  • http://maven.apache.org/plugins/maven-shade-plugin/
  • <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>analyze</id> <phase>verify</phase> <goals> <goal>analyze-only</goal> </goals> <configuration> <failOnWarning>true</failOnWarning> </configuration> </execution> </executions></plugin>
  • 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

    ×