Differences

This shows you the differences between two versions of the page.

Link to this comparison view

maven [2021/04/05 11:23] (current)
Line 1: Line 1:
 +====== Maven ======
  
 +===== Update Parent POM Version in Modules =====
 +If the project structure is the following:
 +<code>
 + Parent POM
 +     +
 +     |
 +     +---> Module 1
 +     |
 +     +---> Module 2
 +     |
 +     +---> Module 3
 +</code>
 +
 +and the modules inherit the version from the parent pom then it is a PITA to update all modules with the new parent pom version when the parent pom version is incremented.
 +
 +An easy way for this is the following command which will update the parent pom version in the modules with the version in the parent pom project.
 +
 +  mvn -N versions:update-child-modules
 +
 +<note tip>By default this command will generate a backup version of each updated pom. To drop the generation of backups add the parameter ''generateBackupPoms=false'' to the maven command.</note>
 +
 +Found on [[http://www.mojohaus.org/versions-maven-plugin/examples/update-child-modules.html|Mojohaus.org]].
 +
 +===== Manifest.merge Error in Eclipse =====
 +
 +  org.codehaus.plexus.archiver.jar.Manifest.merge(org.codehaus.plexus.archiver.jar.Manifest)
 +  
 +This error only occurs in Eclipse. The Eclipse Plugin for the Maven Archiver needs to be updated with this update site:
 +
 +  http://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-mavenarchiver/0.17.0/N/LATEST/
 +
 +See http://stackoverflow.com/questions/28351678/org-codehaus-plexus-archiver-jar-manifest-mergeorg-codehaus-plexus-archiver-jar
 +  
 +  
 +===== Copy Maven Artifact on Build =====
 +<sxh xml;title: pom.xml>
 +<build>
 + <plugins>
 + <plugin>
 + <groupId>org.codehaus.mojo</groupId>
 + <artifactId>wagon-maven-plugin</artifactId>
 + <version>1.0-beta-5</version>
 + <executions>
 + <execution>
 + <id>upload-jar-to-folder</id>
 + <phase>deploy</phase>
 + <goals>
 + <goal>upload</goal>
 + </goals>
 + </execution>
 + </executions>
 + <configuration>
 + <fromDir>${project.build.directory}</fromDir>
 + <includes>${project.build.finalName}.${project.packaging}</includes>
 + <url>file:///opt/karaf</url>
 + <toDir>deploy</toDir>
 + </configuration>
 + </plugin>
 + </plugins>
 +</build>
 +</sxh>
 +
 +===== Add 3rd Party Library =====
 +Sometimes a 3rd party Java library is needed in a Maven project. But the 3rd party jar is not a Maven artifact. Maven has a command for adding those jars to your local repo.
 +
 +<code>
 +mvn install:install-file -Dfile=/tmp/jtopen.jar \
 +    -DgroupId=jtopen \
 +    -DartifactId=jtopen \
 +    -Dversion=7.10 \
 +    -Dpackaging=jar
 +</code>
 +
 +===== Skip Unit test =====
 +
 +Use the Java property ''-Dmaven.test.skip=true'' to skip unit tests.
 +
 +===== Embed Dependencies =====
 +If all dependencies should be embedded into one jar then the Maven Bundle Plugin can be used. All dependencies which should be installed as their own bundle should be configured as ''scope: provided''. All dependencies which should be embedded should be configured as ''scope: compile''.
 +
 +  <Embed-Dependency>*;scope=compile</Embed-Dependency>
 +
 +To embed all transitive dependencies the following configuration must be added to the pom.xml:
 +
 +  <Embed-Transitive>true</Embed-Transitive>
 +
 +The Maven Bundle Plugin embeds all configured dependencies and adjusts the Bundle-ClassPath in the Manifest file.
 +
 +==== Example ====
 +<sxh xml>
 +  <build>
 +    <plugins>
 +      <plugin>
 +        <groupId>org.apache.felix</groupId>
 +        <artifactId>maven-bundle-plugin</artifactId>
 +        <configuration>
 +          <instructions>
 +            <Embed-Dependency>*;scope=compile</Embed-Dependency>
 +            <Embed-Transitive>true</Embed-Transitive>
 +          </instructions>
 +        </configuration>
 +      </plugin>
 +    </plugins>
 +  </build>
 +</sxh>
 +
 +===== Exec Maven Plugin =====
 +Using the [[http://mojo.codehaus.org/exec-maven-plugin/ | exec-maven-plugin]] a Java application can be executed with maven from the command line.
 +
 +<sxh xml>
 +  <plugin>
 +    <groupId>org.codehaus.mojo</groupId>
 +    <artifactId>exec-maven-plugin</artifactId>
 +    <version>1.2.1</version>
 +    <configuration>
 +        <mainClass>de.sgbs.xeno.klax.ImportApp</mainClass>
 +        <arguments>
 +            <argument>-i</argument>
 +            <argument>${input.file}</argument>
 +        </arguments>
 +    </configuration>
 +  </plugin>
 +</sxh>
 +
 +==== Maven Goals ====
 +  * exec:exec execute programs and Java programs in a separate process.
 +  * exec:java execute Java programs in the same VM.
 +
 +==== Executing Java Program with Test Resources ====
 +  mvn exec:java -Dexec.classpathScope="test"
 +  
 +==== Passing arguments ====
 +Arguments can be passed to the maven via the Java environment variables (-D arguments).
 +
 +  mvn exec:java -Dinput.file="my_file.txt"
 +  
 +The Java environment variables can be used in maven as a normal variable, like ${var}.
 +
 +==== Passing arguments to main class ====
 +Arguments of the main class can be passed with the //exec.args// Java environment variable.
 +
 +  mvn exec:java -Dexec.args="these arguments are passed to the main class"
 +
 +{{tag>java devel}}