Advanced Programming with Java Why a Build Tool for Java? Apache Ant Enter Apache Maven

Advanced Programming with Java Why a Build Tool for Java? Apache Ant Enter Apache Maven

Advanced Programming with Java Why a build tool for Java? As an application scales in size, its build process must be Apache Maven provides developers with a automated standard set of tools and conventions for building and deploying Java applications. Maven manages the integration of third-party libraries in The UNIX make utility has been around for years a project and provides a plug-in architecture that User specifies the dependencies among various allows new tools and reports to be easily • integrated into a project’s build system. application components in a “makefile” The make program analyzes those dependencies and • Building Java applications with Maven rebuilds components as necessary Don’t have to rebuild the entire product when you • A history of building Java projects only change one source file • Make doesn’t fit well with Java Maven: building, testing, and reporting • Dependencies among Java files are complex • Make is rarely platform independent • Syntax of makefiles is wiggy • Copyright ©2008-2021 by David M. Whitlock. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and full citation on the first page. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or fee. Request permission to publish from [email protected]. Last updated March 14, 2021. 1 2 Apache Ant Enter Apache Maven The Apache Foundation developed Ant in 2000 to build Developers noticed common patterns in the projects that their Java projects they built Ant provides the building blocks for compiling, Running unit tests as part of every build • assembling, and testing a Java project • Generating reports and documentation about the Ant has tasks that invoke tools like javac, javac, and • project • javadoc Versioning the software and deploying it to a central Tasks are grouped together into build targets that are • location so it can be shared • configured in an XML file that specifies source file locations and class paths Apache Maven was developed to provide a standard build environment that could be used by many projects Ant worked very well, but... Each project produces some artifact (jar file, web Each project had its own build procedure with • application, etc.) from source code • different directory layouts and naming conventions Unit tests are fully integrated into the project – Ramp-up time for new developers • Third-party libraries are automatically downloaded Dependencies on third-party libraries had to be • • managed by individual projects and placed on the classpath – Jar files were often checked into CVS along with Artifacts and supporting information (documentation source code • and reports) can be published to a centralized repository for consumption by others – Often difficult to determine which version of the library was used 3 4 Getting started with Maven Layout of a Maven project Maven can be downloaded from Maven defines a standard layout the project’s source files*. http://maven.apache.org +- proj1/ +- pom.xml The mvn command line tool executes Maven +- src/ +- main/ A new Java project can be created using a Maven +- java/ (Base dir for source code) “archetype” +- edu/pdx/cs410J/whitlock/ +- resources/ (Files on classpath) +- META-INF/MANIFEST.MF $ mvn archetype:create \ +- test/ -DgroupId=edu.pdx.cs410J.whitlock \ +- java/ (Base dir for test code) -DartifactId=proj1 +- edu/pdx/cs410J/whitlock/ +- resources/ (Files on test classpath) This generates a very simple Java project that can be +- site/ (Files for web site) built with Maven +- site.xml (Describes site) +- apt/ (Supplementary site files) $ find proj1 -type f +- resources/ proj1/pom.xml +- css/ proj1/src/main/java/edu/pdx/cs410J/whitlock/App.java +- site.css proj1/src/test/java/edu/pdx/cs410J/whitlock/AppTest.java +- images/ +- target/ +- proj1-1.0.jar (Project artifact) pom.xml is the Maven ”Project Object Model” +- surefire-reports/ (Unit test output) configuration file that specifies how the project is built +- site/ (Generated web site) *The layout can be configured, if necessary. 5 6 What can you do with Maven? Looking at pom.xml Maven defines several build “phases” that perform <project xmlns=’’http://maven.apache.org/POM/4.0.0’’ common tasks xmlns:xsi=’’http://www.w3.org/2001/XMLSchema-instance’’ xsi:schemaLocation=’’http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd’’> mvn compile Compiles Java source code in <modelVersion>4.0.0</modelVersion> <groupId>edu.pdx.cs410J.whitlock</groupId> src/main/java <artifactId>proj1</artifactId> mvn test Recompiles necessary source <packaging>jar</packaging> code and runs unit tests <version>1.0-SNAPSHOT</version> mvn package After running unit tests, builds a <name>proj1</name> <url>http://maven.apache.org</url> jar file with the compiled source <dependencies> code <dependency> mvn site Generates a web site for the <groupId>junit</groupId> <artifactId>junit</artifactId> project with information gleaned <version>3.8.1</version> from pom.xml <scope>test</scope> mvn clean Deletes all build artifacts </dependency> mvn eclipse:eclipse Generates an Eclipse project </dependencies> based on the Maven project </project> This POM configures version 1.0-SNAPSHOT of proj1 Build artifacts are placed in the target directory released by the edu.pdx.cs410J.whitlock organization. The project builds a jar file and its tests depend on version 3.8.1 of junit. 7 8 Information about the project Managing dependencies The POM can contain several pieces of information about A project often requires third-party libraries to do its work. the project that Maven uses for various purposes* A POM specifies what libraries the project depends on and Maven downloads them as needed. name The name of the project (often a code <dependency> name such as “Guatemala”) <groupId>junit</groupId> description General description of project <artifactId>junit</artifactId> url URL where users can learn more <version>3.8.1</version> <scope>test</scope> about project </dependency> inceptionYear When project was started The scope element determine which build phase the You can also include information about the project dependency applies to compile (default): library and all of its dependencies Organization that develops the project (name, url) • • are available to all build phases Developers and contributes (id, name, email, role, a • runtime not required for compilation; only needed to picture URL) • execute tests and run project License under which it is distributed provided: only needed at compile time; someone • • else provides it at runtime Bug tracking system, Mailing lists • test: only needed to compile and run tests • Source code repository, Automated build server • system: library is not available in repository • *These are XML elements that are children of the top-level project – Location is provided by the systemPath POM element. element 9 10 Repositories Local Repository Maven specifies a layout for repositories of deployed Maven downloads project dependencies to a local artifacts repository that resides in your home directory: The layout of the repository is based on the artifact’s ${user.home}/.m2/repository • group id, version, and id The JUnit 3.8.1 artifact is located at When resolving a project dependency, Maven will search • junit/junit/3.8.1/junit-3.8.1.jar the local repository before searching other repositories. Running mvn with -o or --offline will prevent it from The repository also contains a copy of the artifact’s • • POM so that artifacts that it depends on can be accessing remote repositories resolved By default, Maven will search for an artifact in a public repository: http://repo1.maven.org/maven2/ A POM can also specify repositories to search: <repositories> <repository> <id>CS410J</id> <name>CS410J Maven Repository</name> <url>http://web.cecs.pdx.edu/~whitlock/repository/maven2</url> </repository> </repositories> 11 12 Build phases Build Goals A Maven build consists of a number of phases (each Each build phase consists of goals that perform some phase depends on the previous) build task like compiling source code, running tests, or building a jar file validate Validates pom.xml and downloads necessary dependencies Each phase has a default goal that depends on the projects packaging (jar, war, etc.) compile Compiles the source code test Tests the source code (can’t require compile compile:compile that code be packaged) test-compile compiler:testCompile package Builds the project artifact (jar file) test surefire:test integration-test Deploys the artifact (e.g. to a web package jar:jar server) for integration testing verify Makes sure that artifact meets quality Maven plugins are artifacts that provide goals to a Maven criteria (coding standards, etc.) build install Copies artifacts to the local Maven repository for use in other local The plugin for compiling Java source has two goals: • main compiler:compile projects one for the code ( ) and other for test code (compiler:testCompile) deploy Publishes artifacts to remote Maven repository for use by others Plugins are configured in the POM and must be • associated with a build phase Each build phase is dependent on the previous Information about the standard Maven plugins can be Running mvn package will invoke validate, found at: • compile, and test http://maven.apache.org/plugins/index.html 13 14 Compiling Source Code Running Tests Running mvn

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us