User Tools

Site Tools


maven

This is an old revision of the document!


Maven

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.

mvn install:install-file -Dfile=/tmp/jtopen.jar \
    -DgroupId=jtopen \
    -DartifactId=jtopen \
    -Dversion=7.10 \
    -Dpackaging=jar

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>

Damit alle transitiven (nicht direkten) Abhängigkeiten auch eingebettet werden muss folgende Anweisung benutzt werden:

<Embed-Transitive>true</Embed-Transitive>

Das Maven Bundle Plugin sorgt dafür, dass die eingebetteten Java Bibliotheken in dem OSGi Bundle landen und passt das Manifest an (Bundle-ClassPath).

Beispiel

  <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>

Exec Maven Plugin

Using the exec-maven-plugin a Java application can be executed with maven from the command line.

  <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>

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"
maven.1393924692.txt.gz · Last modified: 2014/03/04 09:18 by mihael