Skip to content

Instantly share code, notes, and snippets.

@overtomanu
Last active January 4, 2024 05:21
Show Gist options
  • Save overtomanu/1d0225d5f09749c6130cf91c6a2d7594 to your computer and use it in GitHub Desktop.
Save overtomanu/1d0225d5f09749c6130cf91c6a2d7594 to your computer and use it in GitHub Desktop.
Info type Description
Maven standard directory layout └───maven-project ├───pom.xml ├───README.txt ├───NOTICE.txt ├───LICENSE.txt └───src ├───main │ ├───java │ ├───resources │ ├───filters │ └───webapp ├───test │ ├───java │ ├───resources │ └───filters ├───it ├───site └───assembly
Maven standard directory explanation maven-project/pom.xml – defines dependencies and modules needed during the build lifecycle of a Maven project maven-project/LICENSE.txt – licensing information of the project maven-project/README.txt – summary of the project maven-project/NOTICE.txt – information about third-party libraries used in the project maven-project/src/main – contains source code and resources that become part of the artifact maven-project/src/test – holds all the test code and resources maven-project/src/it – usually reserved for integration tests used by the Maven Failsafe Plugin maven-project/src/site – site documentation created using the Maven Site Plugin maven-project/src/assembly – assembly configuration for packaging binaries src/main/java – Java source code for the artifact src/main/resources – configuration files and others such as i18n files, per-environment configuration files, and XML configurations src/main/webapp – for web applications, contains resources like JavaScript, CSS, HTML files, view templates, and images src/main/filters – contains files that inject values into configuration properties in the resources folder during the build phase src/test/java – Java source code for tests src/test/resources – configuration files and others used by tests src/test/filters – contains files that inject values into configuration properties in the resources folder during the test phase
Maven dependency scopes Compile - Default. Available on all classpaths of project. Also, propagated to downstream projects. • Provided - Like Compile, but expected to be provided by JDK or container at runtime. • Runtime - Not required for compile, but needed for runtime. On runtime and test classpaths, not compile. • Test - Only available on test classpath, not transitive. • System - similar to provided, but JAR is added to system explicitly. (via file path) • Import - Imports dependency of POM.
Maven dependency important goals • dependency:tree - shows the dependency tree. Useful for resolving conflicts • dependency:go-offline - Resolve all all, prepare to go offline • dependency:purge-local-repository - Clear artifacts from local repository • dependency:sources - get sources for all dependencies.
Maven coordinates • Maven Coordinates are used to identify artifacts • Together, they identify a ‘location’ in a Maven repository. • groupId - Typically unique to an organization. Often the organization’s reverse domain is used. But not always. Can be just ‘junit’. • artifactId - typically the project name. A descriptor for the artifact • version - refers to a specific version of the project. • groupId and version can be inherited from a parent POM
Maven project versions • Example - 3.2.1-987-beta • Major Version - first number - 3 • Minor Version - second number - 2 • Incremental Version (patch) - third number - 1 • Build Number - from CI Build - 987 • Qualifier - String Qualifier - ‘beta’ • Most common in use is just major.minor.incremental (3.2.1)
Maven snapshot version • Example - 3.2.1-SNAPSHOT • The SNAPSHOT suffix is an important qualifier to Maven behavior • Tells Maven this is a development version • Not stable, and Maven should check for newer versions • Maven will first check locally, then check remote repositories • By default, Maven will check remote repositories once per day • Option is configurable.
Maven build lifecycle • Maven is based on the concept of build lifecycles • A lifecycle is a pre-defined group of build steps called phases • Each phase can be bound to one or more plugin goals • All work done in Maven is done by plugins! • Lifecycles and phases provide the framework to call plugin goals in a sequence
Maven predefined lifecycles • Maven has three pre-defined lifecycles: clean, default, site. • Clean • Does a clean of the project, removes all build artifacts from working directory • Defined with plugin bindings • Default • Does the build and deployment of your project • Defined without plugin bindings, bindings are defined for each packaging • Site • Creates the a website for your project • Defined with plugin bindings • Least used in the enterprise • See any of the Maven websites for examples • All are built using the Maven site lifecycle
Maven clean lifecycle phases Clean lifecycle Phase: pre-clean (no goals mapped) Phase: clean ---> maven-clean-plugin ---> goal clean Phase: post-clean (no goals mapped)
Maven default lifecycle phases High level • validate: validate the project is correct and all necessary information is available. • compile: compile the source code of the project. • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed. • package: take the compiled code and package it in its distributable format, such as a JAR. • integration-test: process and deploy the package if necessary into an environment where integration tests can be run. • verify: run any checks to verify the package is valid and meets quality criteria • install: install the package into the local repository, for use as a dependency in other projects locally. • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. Detailed • 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
Maven default lifecycle - jar packaging • Phase: process-resources - Plugin: maven-resources-plugin : resources • Phase: compile - Plugin: maven-compiler-plugin : compile • Phase: process-test-resources - Plugin: maven-resources-plugin : testResources • Phase: test-compile - Plugin: maven-compiler-plugin : testCompile • Phase: test - Plugin: maven-surefire-plugin : test • Phase: package - Plugin: maven-jar-plugin : jar • Phase: install - Plugin: maven-install-plugin : install • Phase: deploy - Plugin: maven-deploy-plugin : deploy
Maven site lifecycle • Site Lifecycle Phases • Phase: Pre-site - Plugin: none • Phase: Site - Plugin: maven-site-plugin : site • Phase: Post-site - Plugin: none • Phase: Site-Deploy - Plugin: maven-site-plugin : deploy
maven-clean-plugin • Build Lifecycle - CLEAN • Has only one goal - ‘clean’ • Purpose is to remove files generated during build process. • By default removes /target directory project root and submodule root folders
maven-compiler-plugin • Build Lifecycle - DEFAULT • Has two goals - compiler:compile, compiler:testCompile • By Default uses the compiler ‘javax.tools.JavaCompiler • Can be configured to use javac if needed • Default source and target language levels are Java 1.6 • Apache team encourages these values to be set
maven-resources-plugin • Build Lifecycle - DEFAULT • Has 3 goals - resources:resources, resources:testResources, resources:copy-resources • Purpose is to copy project resources to output directory (target dir) • Can be configured for encoding, source and target directories • Rather versatile configuration options for copying files during build processing
maven-surefire-plugin • Build Lifecycle - DEFAULT • Has one goal: surefire:test • The Surefire plugin is used to execute unit test of the project. • By default supports JUnit 3, JUnit 4, JUnit 5, and TestNG • Cucumber runs under JUnit, Spock compiles to JUnit byte code. • By default includes classes named: • */Test.java; **/*Test.java; **/*Tests.java; **/*TestCase.java
maven-jar-plugin • Build Lifecycle - DEFAULT • Has two goals: jar:jar, jar:test-jar • Purpose is to build jars from complied artifacts and project resources • Can be configured for custom manifests, and to make executable jars
maven-deploy-plugin • Build Lifecycle - DEFAULT • Has two goals - deploy:deploy, deploy:deploy-file • Purpose is to deploy project artifacts to remote Maven repositories • Often done in CI • Configuration is typically part of the Maven POM
maven-site-plugin • Build Lifecycle - SITE Has 7 goals: • site:site - Generate site for project • site:deploy - Deploy site via Wagon • site:run - Run Site locally using Jetty as web server • site:stage - generate site to a local staging directory • site:stage-deploy - Deploy site to remote staging location • site:attach-descriptor - adds site.xml (site map file used by search engines) to files for deployment • site:jar - bundles site into a jar for deployment to a repository • site:effective-site - generates the site.xml file
flatten-maven-plugin creates flatten-pom by substituting variables used to specify version property of dependencies/modules with their actual values
maven-enforcer-plugin can fail builds based on rules like require specific OS or require specific maven version range etc
maven-shade-plugin used for create uber jar (fat jar) with main class (executable jar)
TODO: maven create uber jar install original jar to repository
Command Description
mvn -N io.takari:maven:wrapper Create maven project specific wrapper script The option -N means –non-recursive so that the wrapper will only be applied to the main project of the current directory, not in any submodules. After executing the goal, we'll have more files and directories in the project: mvnw: it's an executable Unix shell script used in place of a fully installed Maven mvnw.cmd: it's the Batch version of the above script mvn: the hidden folder that holds the Maven Wrapper Java library and its properties file
mvn -N io.takari:maven:wrapper -Dmaven=3.5.2 Create maven project specific wrapper script by specifying maven version to be used
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-simple -DarchetypeVersion=1.4 Create maven project from archetype
mvn archetype:generate list available archetypes then create project by choosing one of them
mvn clean delete target directory
mvn validate validate, if the project is correct
mvn compile compile source code, classes stored in target/classes
mvn test run tests
mvn package take the compiled code and package it in its distributable format, e.g. JAR, WAR
mvn package -P,! Run package with profile1 active and profile2 disabled (if profile2 is active by default)
mvn verify run any checks to verify the package is valid and meets quality criteria, runs integration tests
mvn clean verify -DskipTests package and verify without running tests
mvn clean verify -DskipITs package and verify without running integration tests
mvn install Compile, package and install jar in local repository
mvn deploy copies the fnal package to the remote repository
mvn help:describe used to get relative information about a project or the system
mvn help:effective-pom displays the effective POM as an XML for the current build, with the active profles factored in
mvn dependency:analyze analyzes the dependencies of this project
mvn dependency:tree prints a tree of dependencies
mvn help:active-profiles show active maven build profiles
mvn release:update-versions -DautoVersionSubmodules=true Update pom versions
mvn clean release:prepare Prepare to do release of maven project. Performs check to see if there are any uncomitted code changes, if project is dependent on any snapshot versions of dependency etc, tags release code in source code management
mvn clean release:prepare -DdryRun=true simulate maven release run, generates maven pom for next release, tagged release and also backup current
mvn release:perform performs the release. checks out tagged release branch, builds it and pushes it to remote repository. Also removes temporary files created in release prepare.
mvn release:clean cleans up file after release:prepare fails due to validation errors for releasing
mvn versions:display-dependency-updates shows if newer versions of library dependencies are released
mvn versions:use-releases convert dependencies on snapshot libraries to their release if available (converts only snapshot dependencies)
mvn versions:use-next-releases port every non snapshot dependency to nearest version
mvn versions:use-latest-releases Update every non-SNAPSHOT dependency to its latest release
mvn versions:lock-snapshots temporarily replace the -SNAPSHOT with a locked -YYYYMMDD.HHMMSS-NNN snapshot. Helpfull if you need a short term semi-reproducible build
mvn versions:commit accept the modifications made to your pom.xml files by the versions-maven-plugin
mvn versions:revert restore your pom.xml files to their initial state, before you started modifying it with the versions-maven-plugin
mvn release:prepare -DdryRun=true Prepare for a release in SCM. dryrun means it just simulates
mvn -Dusername=your_scm_username clean release:prepare Prepare for a release in SCM.
mvn --batch-mode release:prepare Prepare for release by using the default inputs for the versions and tag information. Does not prompt for any values.
mvn release:perform Perform a release from SCM.
mvn release:rollback Rollback a previous release.
mvn release:branch performs same steps as the release:prepare goal, creating a branch instead of a tag
mvn release:update-versions performs the normal version updates of the release:prepare goal without making other modifications to the SCM such as tagging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment