How to set classpath for mvn exec:exec?

前端 未结 3 634
刺人心
刺人心 2020-12-31 22:48

I\'m trying to have mvn exec:exec (or mvn exec:java) run my program with a local jar in the classpath. However the jar fails to load:



        
相关标签:
3条回答
  • 2020-12-31 23:22

    To set additional classpath in maven you should use in your maven configuration file as below:

    <additionalClasspathElements>
        <additionalClasspathElement>path/to/additional/jar</additionalClasspathElement>
    </additionalClasspathElements>
    

    For more detail: http://maven.apache.org/surefire/maven-surefire-plugin/examples/configuring-classpath.html

    0 讨论(0)
  • standard java does not allow us to specify multiple -cp arguments, but exec-maven-plugin does, so

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution><goals><goal>exec</goal></goals></execution>
        </executions>
        <configuration>
          <executable>./java.pl</executable>
          <arguments>
            <argument>-ea</argument>
            <argument>-cp</argument><argument>.</argument>
            <argument>-cp</argument><argument>my.jar</argument>
            <argument>-cp</argument><classpath/>
            <argument>org.example.ConfigByXml</argument>
          </arguments>
        </configuration>
      </plugin>
    

    note the call to java.pl above, this is the trick

    #!/usr/bin/env perl
    while (@ARGV) {
        $arg = shift;
        if ($arg eq '-cp' or $arg eq '-classpath') {
            push @cp, shift;
            next;
        }
        push @args, $arg;
    }
    unshift @args, 'java', '-cp', join(':', @cp);
    # print 'args: ', join(' --- ', @args); # uncomment to debug
    exec @args;
    

    understand what java.pl does and use it or do the equivalent in bash, cmd, powershell, whatever..

    0 讨论(0)
  • 2020-12-31 23:46

    In maven it is possible to include a local jar (which is outside of maven repository) using systemPath. But since the scope is system (for dependencies declared with systemPath), there are few limitations and because of that it only works with exec:java.

    For exec:exec, the above solution will not work because maven does not include system scoped dependencies in its generated (runtime) classpath (%classpath) so the solution is to use your own classpath instead of maven generated classpath as shown below.

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                         <argument>-classpath</argument>
                         <argument>local.jar;target/project-jar-with-dependencies.jar</argument>
                         <argument>xpress.audio.AudioProducer</argument>
                    </arguments>
                </configuration>
            </plugin>
    

    Replace local.jar with all the jar files that are required to be present at some fixed location (here project root is assumed, where pox.xml is located). Also note the use of 'project-jar-with-dependencies.jar', in your case it should be target\XpressAudio-1.0-SN APSHOT-jar-with-dependencies.jar.

    0 讨论(0)
提交回复
热议问题