Java Builds with Maven and Ant

4,464 views
4,256 views

Published on

Overview of Maven and Ant for Java builds, with some comparison and contrast.

Published in: Technology
1 Comment
6 Likes
Statistics
Notes
  • There's one level missing from the Maven directory structure.
    It should look like this instead:

    src/main/java/com/example/app/App.java
    src/test/java/com/example/app/AppTest.java

    Note the additional /java/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
4,464
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
336
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide

Java Builds with Maven and Ant

  1. 1. Pasadena Java Users Group pasadenajug.org Java Builds with Ant & Maven Feb 23, 2009 David Noble dnoble@g2ix.com
  2. 2. About Me David Noble Sr Solutions Architect Exist Global / G2iX
  3. 3. About You Ant users? Ivy?  Maven users?  Continuous integration? 
  4. 4. Build Tool Collect inputs  Process inputs  Generate final outputs 
  5. 5. Build Tool - Inputs Source code  Java, SQL  Resources  Properties, XML  Dependencies  Libraries, components, frameworks 
  6. 6. Build Tool - Processing Compile  Run tests  Copy files  Replace keywords  Package  Install / deploy 
  7. 7. Build Tool - Outputs JAR / WAR / EAR files  Zip files  Reports 
  8. 8. Ant Maven Procedural Declarative 1. Set path 1. Define name & version 2. Compile jars 2. Define as WAR project 3. Run unit tests 3. Specify dependencies 4. Create reports 4. Specify unit test plugin 5. Copy files 6. Assemble WAR
  9. 9. Ant “make for java”  Properties, paths, and filesets  Targets  Target dependencies  Tasks 
  10. 10. Ant Installation Download  Unzip  Add to PATH  Set ANT_HOME env variable  Extend by adding jars to lib/  Customize by modifying XSL in etc/ 
  11. 11. Ant Project Definition build.xml Project name  Property variables  Paths  Tasks  Targets 
  12. 12. Ant Properties <property name=quot;lib.dirquot; value=quot;libquot;/> From command line  In build.xml  From external XML  From external property files  From environment 
  13. 13. Ant Paths <path id=quot;classpathquot;> <fileset dir=quot;${lib.dir}quot; includes=quot;**/*.jarquot;/> </path> <target name=quot;compilequot;> <mkdir dir=quot;${classes.dir}quot;/> <javac srcdir=quot;${src.dir}quot; destdir=quot;${classes.dir}quot; classpathref=quot;classpathquot;/> </target>
  14. 14. Ant Targets Name  Description  Dependencies  Conditionals (if, unless)  <antcall> task 
  15. 15. Ant Tasks Core tasks  Optional tasks  Custom tasks  Third party  Write your own 
  16. 16. Selected Core Tasks Ant AntCall Apply Basename BuildNumber Checksum Chmod Concat Condition Copy Cvs Delete DependSet Dirname Ear Echo EchoXML Exec Fail Filter FixCRLF Get Import Input Jar Java Javac Javadoc Length LoadFile LoadProperties LoadResource MakeURL Mail MacroDef Manifest ManifestClassPath Mkdir Move Nice Parallel Patch PathConvert PreSetDef Property Record Replace ResourceCount Retry Rmic Sequential SignJar Sleep Sql Subant Sync Tar Taskdef Tempfile Touch Truncate TStamp Typedef Unjar Untar Unwar Unzip Uptodate Waitfor WhichResource War XmlProperty XSLT Zip
  17. 17. Ant Multi-Project Builds Option 1 – Master build.xml  Option 2 – Cascading build.xml's 
  18. 18. Ant Scripting <macrodef> task  <script> task 
  19. 19. Ivy for Dependencies build.xml <project xmlns:ivy=quot;antlib:org.apache.ivy.antquot; name=quot;hello-ivyquot; default=quot;runquot;> <target name=quot;resolvequot;> <ivy:retrieve /> </target> </project>
  20. 20. Ivy for Dependencies ivy.xml <ivy-module version=quot;2.0quot;> <info organisation=quot;apachequot; module=quot;hello-ivyquot;/> <dependencies> <dependency org=quot;commons-langquot; name=quot;commons-langquot; rev=quot;2.0quot;/> <dependency org=quot;commons-cliquot; name=quot;commons-cliquot; rev=quot;1.0quot;/> </dependencies> </ivy-module>
  21. 21. Ivy Repositories Maven artifact repositories  Build your own 
  22. 22. Ivy Reports
  23. 23. Ant Ecosystem IDE plugins  Custom tasks 
  24. 24. More Ant Ant http://ant.apache.org/ Core tasks http://ant.apache.org/manual/coretasklist.html External tools http://ant.apache.org/external.html IVY http://ant.apache.org/ivy/
  25. 25. Maven Project and artifact-based build  platform Uses repositories to manage  artifacts and third party libraries Customized by modifying the  behavior of the life-cycle via plugins.
  26. 26. Maven
  27. 27. Maven Artifacts Output of the build process  JARs, WARs, EARs, zip files  Stored in repositories  Published and consumed by Maven  One per project 
  28. 28. Maven Dependencies Items needed during build process  Managed as artifacts and metadata  within remote repositories Usually created and published by  other Maven builds Though not necessarily 
  29. 29. Maven Dependencies <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.2</version> <scope>test</scope> </dependency>
  30. 30. Maven Directory Structure pom.xml  src/  target/ 
  31. 31. Maven Directory Structure src  main  com/example/app/App.java  test  com/example/app/AppTest.java 
  32. 32. Maven Directory Structure target  classes/  generated-classes/  site/  surefire-reports/  test-classes/  webdemo-1.0-SNAPSHOT.war 
  33. 33. Maven Project Object Model “POM”  Fundamental unit of work in Maven  Build components inherit from best  practices and common base
  34. 34. Maven Build Life-Cycle Consistent pattern  Portable process knowledge  Modify build with plugins  Extensible in decoupled way  Large number of phases, not all  required Compile, test, package, install, deploy 
  35. 35. Default Lifecycle Phases validate, initialize  generate-sources, process-sources  generate-resources, process-resources  compile, process-classes  generate-test-sources, process-test-sources  generate-test-resources, process-test-resources  test-compile, process-test-classes  test  prepare-package, package  pre-integration-test, integration-test, post-integration-test  verify, install, deploy 
  36. 36. Site Lifecycle Phases Pre-site: executes processes  Site: generates documentation  Post-site: executes processes  Site-deploy: copies files 
  37. 37. Clean Lifecycle Phases Pre-clean: executes processes  Clean: removes files  Post-clean: executes processes 
  38. 38. Phases have Goals generate-resources ear:generateApplicationXml process-resources resources:resources package ear:ear install install:install deploy deploy:deploy
  39. 39. Maven Settings Two setting files  Installation settings:  conf/settings.xml in install directory  User settings: ~/.m2/settings.xml  C:Usersusername on Windows Vista  C:Documents and Settingsusername on XP 
  40. 40. Maven Settings Separate installation and user settings User settings are things you don't share  server usernames and passwords, local paths, etc.  Installation settings are things that should  be the same on all Maven installations can distribute various ways: check out Maven install  from SCM, provide a re-bundled Maven, etc.
  41. 41. Maven Archetypes POJO jar  Web applications  AppFuse, Wicket, MyFaces, etc  Groovy 
  42. 42. Maven Archetypes mvn archetype:generate  Application structure  Code generation  Plugin  Distributed in repositories 
  43. 43. Maven Resources Accessed by code at runtime  Not required for compilation  Packaged within the artifact  Can be filtered with custom values 
  44. 44. Maven Plugins Reusable pieces of build functionality  Perform a clearly defined task based on a set of  input configuration Maven takes responsibility for orchestrating their  execution Primarily written in Java, but can be written in a  number of scripting languages Are also stored and downloaded from repositories 
  45. 45. Maven Repositories Repositories are a storage location for artifacts  Maven has two types:  Remote repository - repository accessible over HTTP, FTP, filesystem, etc that  contains artifacts to consume and as a place to publish build artifacts Local repository - a cache of artifacts on your machine, downloaded from  several remote repositories Maven is pre-configured to use the Central Repository for  dependencies A large collection of open source and re-distributable artifacts,  primarily for Java Can establish and manage your own remote repositories  Can be used by build systems other than Maven 
  46. 46. Repository Servers Archiva  Artifactory  Nexus 
  47. 47. Archiva Helps to manage Maven repositories  Control your environment for your  team Can be run personally for offline  cache
  48. 48. Archiva A repository management server  manages Maven remote repositories and the artifacts inside them  Used by a group or team for an “internal” remote repository  deploy your own build artifacts  control content from external sources  on demand downloading of artifacts  CI server companion  Can be used as a personal server  make it easier to clean your local repository without downloading remotely again  must be careful about introducing build inconsistencies localized to your  environment, however
  49. 49. Installing Archiva Two choices  Existing servlet container or application server  Standalone  pre-configured instance of Jetty 6, Java Service Wrapper, Apache Derby  Configuration files  APP_BASE/conf/jetty.xml - server port, outgoing mail server and databases  standalone only, configure using container settings for others  APP_BASE/conf/archiva.xml - maintained by the application  applies to both choices  Two databases – users and repository statistics  When using standalone, recommend a separate conf directory  set ARCHIVA_BASE environment variable to point to a copy of the conf directory  makes upgrading easier 
  50. 50. Configuring Maven For Archiva There are a number of alternatives for configuring Maven to use a repository  manager: add to the POM  used in addition to central, but always checked first  add to Maven settings  as above, but applies to all projects  override or mirror central  never use the central repository, everything else the same  override or mirror all repositories given  Could add the repositories to the POM  what if the parent POM is in the repository, not the check out?  use the POM if users must build using that repository without configuring settings  Use Maven settings instead  generally prefer the mirror all repositories 
  51. 51. Maven Archetypes  Artifacts  POM – Project Object Model  Dependencies  Directory structure  Plugins (testing, reporting, deploying, etc)  Repositories 
  52. 52. Maven Ecosystem IDE support  Artifact repositories  Plugins  Continuous integration 
  53. 53. Maven Companies
  54. 54. Maven Links Maven http://maven.apache.org/ Archiva http://archiva.apache.org/ Artifacts http://mvnrepository.com/ Free Books http://www.exist.com/better-build-maven/ http://www.sonatype.com/book
  55. 55. Credits Special thanks to Carlos Sanchez and Brett Porter for their inputs and material from their training slides
  56. 56. Thanks! David Noble dnoble@g2ix.com www.dnoble.org

×