如何设置mvn exec的类路径:exec?

时间:2013-09-28 07:28:39

标签: maven classpath exec

我试图让mvn exec:exec(或mvn exec:java)在类路径中使用本地jar运行我的程序。但是jar无法加载:

Exception in thread "main" java.lang.Error: Unable to load voice directory. java.lang.ClassNotFoundException: com.sun.speech.freetts.en.us.cmu_us_slt_arctic.ArcticVoiceDirectory
at com.sun.speech.freetts.VoiceManager.getVoiceDirectories(VoiceManager.java:211)
at com.sun.speech.freetts.VoiceManager.getVoices(VoiceManager.java:111)
at com.sun.speech.freetts.VoiceManager.getVoice(VoiceManager.java:521)
at xpress.audio.TTS.<init>(TTS.java:66)
at xpress.audio.TTS.<init>(TTS.java:62)
at xpress.audio.AudioProducer.main(AudioProducer.java:18)

使用java直接从CLI运行程序:

    C:\XpressAudio\target\classes>java -cp "C:\XpressAudio\target\XpressAudio-1.0-SN
APSHOT-jar-with-dependencies.jar;C:\XpressAudio\cmu_us_slt_arctic.jar;C:\XpressA
udio\en_us.jar;C:\XpressAudio\*" xpress.audio.AudioProducer

这是<build>的{​​{1}}部分:

pom.xml

有人可以告诉我如何编辑 <build> <plugins> <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> <mainClass>xpress.audio.AudioProducer</mainClass> </configuration> <dependencies> <dependency> <groupId>cmu_us</groupId> <artifactId>slt_arctic</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/cmu_us_slt_arctic.jar</systemPath> </dependency> </dependencies> </plugin> </plugins> </build> 以使pom.xml的工作方式与上面的mvn exec:exec命令相同?

javacom.sun.speech.freetts.en.us.cmu_us_slt_arctic.ArcticVoiceDirectory

中的一个班级

3 个答案:

答案 0 :(得分:5)

在maven中,可以使用systemPath包含本地jar(在maven存储库之外)。但由于范围是系统(对于使用systemPath声明的依赖关系),因此几乎没有限制,因此它只适用于exec:java。

对于exec:exec,上面的解决方案不起作用,因为maven在其生成的(运行时)类路径(%classpath)中不包含系统范围的依赖项,因此解决方案是使用您自己的类路径而不是maven生成的类路径,如下所示

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

将local.jar替换为某些固定位置所需的所有jar文件(假设项目根目录为pox.xml所在的位置)。还要注意使用'project-jar-with-dependencies.jar',在你的情况下它应该是target \ XpressAudio-1.0-SN APSHOT-罐与 - dependencies.jar。

答案 1 :(得分:3)

标准java不允许我们指定多个-cp参数,但exec-maven-plugin会这样做,所以

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

注意上面对java.pl的调用,这是诀窍

#!/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;

了解java.pl做了什么,并使用它或在bash,cmd,powershell等等中做等效的事情。

答案 2 :(得分:0)

要在maven中设置其他类路径,您应该在maven配置文件中使用,如下所示:

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

更多细节: http://maven.apache.org/surefire/maven-surefire-plugin/examples/configuring-classpath.html