Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a jar to maven exec:java classpath

Tags:

java

maven

java-7

I have a batch file which runs a java class using maven which depends on tools.jar (from the JDK).
For example:
mvn -f .\pom.xml -e exec:java -Dfile.encoding="UTF-8" -Dexec.mainClass=MyClass -Dexec.args="%1 %2 %3 %4 %5 %6 %7 %8 %9" -Dexec.classpathScope=runtime
My program uses tools.jar from the JDK and I've added a system dependency in maven which points to it.
Since the exec:java goal doesn't include system dependencies, I want to add the dependency from the command line manually.
Although I expected it to be trivial I could find the way to do it. Any help will be appreciated.
Thanks,
Avner

like image 684
Avner Levy Avatar asked Jul 29 '12 15:07

Avner Levy


People also ask

How do I run a Java class in a jar classpath?

To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar. We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …]

Does Maven add to classpath?

Maven Archiver can add the classpath of your project to the manifest. This is done with the <addClasspath> configuration element.


1 Answers

From what I read at maven exec plugin it allow you to configure you executable dependencies as plugin dependencies.

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <configuration>
          <includeProjectDependencies>false</includeProjectDependencies>
          <includePluginDependencies>true</includePluginDependencies>
          <executableDependency>
            <groupId>com.example.myproject</groupId>
            <artifactId>mylib</artifactId>
          </executableDependency>
          <mainClass>com.example.Main</mainClass>
        </configuration>
        <dependencies>
          <dependency>
                <groupId>sun.jdk</groupId>
                <artifactId>tools</artifactId>
                <version>1.5.0</version>
                <scope>system</scope>
                <systemPath>${java.home}/../lib/tools.jar</systemPath>
          </dependency>
        </dependencies>
      </plugin>
like image 68
Tomasz Krzyżak Avatar answered Sep 18 '22 08:09

Tomasz Krzyżak