Maven
Maven tips: the build lifecycle, overriding parameters, skipping tests, dependency graphs and archetypes.
6 min read
References
Maven Build Lifecycle
- Validate: validate the project and its information
- Compile: compile the source code
- Test: compile test code and run tests
- Package: package the project into jar or war (or other compressed) files
- Verify: verify results of integration tests
- Install: install the files in ‘package’ stage to a local repository
- Deploy: deploy the packages to a remote repository for sharing with other developers
When you run a command such as mvn [phase] , Maven will execute all phases from validate to the specified [phase]
How to install maven with brew but without openjdk
brew install --cask temurin17cd /usr/local/optln -s /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home openjdkbrew install --ignore-dependencies mavenSending or overriding parameters to pom
-D option is to send parameter value to the pom, or to override the values in ~/.m2/settings.xml.
$ mvn exec:exec -DcustomerId=1234 -Doutcome=REJECTED -DserviceUrl=http://localhost:8080/services/soapSkipping tests, gpg and javadoc
$ mvn clean install -DskipTests -Dgpg.skip -Dmaven.javadoc.skip=trueMaven Dependency Graph
Open pom.xml and go to the Graph tab to see dependency graph.
Creating maven archetypes
Generate the base-archetype
cd into the project directory (or, in the case of a multi-module project, cd into the parent/aggregator project) and run the following command:
$ mvn archetype:create-from-project$ mvn cleanThis will generate a bulk of files and folders under the target/generated-sources/archetype directory: From the folder structure you can discard the target directory sub-tree, just mvn clean it.
$ cd target/generated-sources/archetype$ tree -d.└── src ├── main │ └── resources │ ├── META-INF │ │ └── maven │ └── archetype-resources │ └── src │ └── main │ ├── java │ └── webapp │ └── WEB-INF └── test └── resources └── projects └── basicAdjusting the archetype
${rootArtifactId}to rename variables, methods, comments, etc that depends on the supplied project name.__rootArtifactId__to rename files/folders.
How to add a local library to a maven project in NetBeans?
- Right-click on the “Dependencies” folder in the Projects view
- Enter some groupId, artifactId and version information
- Dependency will be added to the pom.xml and will appear under “Libraries” node of maven project
- Right-click lib node and “manually install artifact”, fill the path to the jar
- Jar should be installed to local Maven repo with coordinates entered in step 2
Netbeans will issue the following mvn command behind the scenes
mvn -DartifactId={artifact id} -DgroupId={group id} -Dversion={version} -Dpackaging=jar -Dfile={path to jar} -DgeneratePom=false install:install- fileInject Maven project informations into Java application
http://ovaraksin.blogspot.com/2012/02/inject-maven-project-informations-into.html
Install local jar with the maven-install-plugin
http://www.baeldung.com/install-local-jar-with-maven/
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> <configuration> <groupId>net.sf.barcode4j</groupId> <artifactId>barcode4j-light</artifactId> <version>2.1</version> <packaging>jar</packaging> <file>${basedir}/dependencies/barcode4j-light.jar</file> <generatePom>true</generatePom> </configuration> <executions> <execution> <id>install-jar-lib</id> <goals> <goal>install-file</goal> </goals> <phase>validate</phase> </execution> </executions> </plugin> </plugins></build>Suppressing GPG signing for Maven builds
Add gpg plugin in a profile as below and then when you need to do a release, add the property to your mvn command:
mvn -DperformRelease=true ...<profiles> <profile> <id>release-sign-artifacts</id> <activation> <property> <name>performRelease</name> <value>true</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile></profiles>OSSRH Sonatype Nexus
Maven always uses either one or two settings files. The global settings defined in ${M2_HOME}/conf/settings.xml is always required. The user settings file defined in ${user.home}/.m2/settings.xml is optional. Any settings defined in the user settings take precedence over the corresponding global settings.
<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd"> <servers> <server> <id>gpg.passphrase</id> <passphrase>P@assphrase123</passphrase> </server> </servers></settings><build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins></build>First if you do not have one, you need to create a key. To generate a key pair
gpg --gen-keyList all available keys
gpg --list-keysand send key to pool.sks-keyservers.net server
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys [key]To check the configuration
mvn clean verifyPossible errors you will get are
gpg: signing failed: Inappropriate ioctl for device
Add below line to ~/.bash_profile or ~/.zshrc
GPG_TTY=$(tty)export GPG_TTYand reload your .zshrc
source ~/.zshrcor
. ~/.zshrcgpg: keyserver receive failed: No route to host
List all available gpg servers
gpg-connect-agent --dirmngr 'keyserver --hosttable'and select and use an active server in order to send or receive key
gpg --keyserver hkp://keys.openpgp.org --send-keys [key]gpg --keyserver hkp://keys.openpgp.org --recv-keys [key]
gpg --keyserver hkp://pgp.mit.edu --send-keys [key]gpg --keyserver hkp://pgp.mit.edu --recv-keys [key]git commit signing failed: secret key not available
You need to configure the secret key before using it.
git config user.signingkey 35F5FFB2Or declare it globally if you want to use the same key for every repository.
git config --global user.signingkey 35F5FFB2maven-glassfish-plugin
To deploy your application using Maven, create the settings.xml file under the .m2 folder, providing the glassfish profile credentials: they will automatically imported in the pom.xml file.
in your pom.xml file add
<build> <plugins> <plugin> <groupId>org.glassfish.maven.plugin</groupId> <artifactId>maven-glassfish-plugin</artifactId> <version>2.1</version> <configuration> <glassfishDirectory>${glassfish.home}</glassfishDirectory> <user>admin</user> <!--<adminPassword>${glassfish.adminPassword}</adminPassword>--> <passwordFile>${glassfish.passfile}</passwordFile> <domain> <name>domain1</name> <httpPort>8080</httpPort> <adminPort>4848</adminPort> </domain> <components> <component> <name>${project.artifactId}</name> <artifact>target/${project.build.finalName}.war</artifact> </component> </components> <debug>true</debug> <terse>false</terse> <echo>true</echo> </configuration> </plugin> </plugins></build>and then run
$ mvn clean package glassfish:deploywildfly-maven-plugin
in your pom.xml file add
<build> <plugins> <plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-maven-plugin</artifactId> <version>2.0.0.Final</version> </plugin> </plugins></build>and then run
$ mvn clean wildfly:deployexec-maven-plugin
to backup mysql database for example, add below lines in your pom.xml
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <id>default-cli</id> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>/usr/bin/mysqldump</executable> <workingDirectory>/tmp/${database.schema}</workingDirectory> <commandlineArgs>${database.schema} --host ${database.host} --user=${database.username} --password=${database.password} --result-file=${env.name}_${database.schema}_backup.sql</commandlineArgs> </configuration> </plugin> </plugins></build>and then run
$ mvn exec:exec -Pproductionmaven-clean-plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>3.2.0</version> <configuration> <filesets> <fileset> <directory>${basedir}/data</directory> </fileset> <fileset> <directory>${basedir}/</directory> <includes> <include>**/*.dmg</include> <include>**/*.tmp</include> <include>**/*.log</include> </includes> <excludes> <exclude>**/important.log</exclude> <exclude>**/another-important.log</exclude> </excludes> <followSymlinks>false</followSymlinks> </fileset> </filesets> </configuration></plugin>Dependency tree
mvn dependency:treeCheck new version of the dependencies
mvn versions:display-dependency-updatesmvn:deploy
This runs through the whole build life cycle and therefore performs the following tasks:
- compile main and test Java code
- run tests
- create a Java archive JAR with the compiled code and resources
- create a JAR containing the source code
- create JavaDoc HTML files
- create a JAR containing JavaDocs
- sign all artifacts with GPG
- install them to the local repository
The version number determines the deployment target. Any version number ending in -SNAPSHOT is deployed to the OSSRH snapshot repository. The exact URL including the path from the groupId, artifactId and version is e.g. https://oss.sonatype.org/content/repositories/snapshots/com/aripd/component/1.6-SNAPSHOT/.
Any other version number constitutes a release and is deployed to a OSSRH staging repository for potential synchronization to the Central Repository. You can log into OSSRH, verify the components and manually release them as desired.
The actual invocation used in the Pipelines script is
mvn -V -B -s settings.xml deployThe -V options triggers an output of the Maven and Java versions at the beginnging of the build. The -B option suppresses the download progress logging for each required artifact. The -s settings.xml configuration causes the usage of the local settings with the required credentials.