2013-09-28 94 views
2

我试图让mvn exec:exec(或mvn exec:java)在类路径中使用本地jar运行我的程序。但是罐子加载失败:如何为mvn exec:exec设置classpath?

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) 

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

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 

这里是我的pom.xml<build>部分:

<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的工作方式与java com相似上面的任务?

com.sun.speech.freetts.en.us.cmu_us_slt_arctic.ArcticVoiceDirectorycmu_us_slt_arctic.jar

+0

[Maven exec插件 - 如何包含“系统”类路径?](http://stackoverflow.com/questions/5286279/maven-exec-plugin-how-to-include-system-classpath) – Joe

+0

您可能应该使用java mojo而不是exec mojo,并查看其中列出的选项:http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html。特别是可执行的依赖项,包括我猜测包括插件依赖和includeProjectDependencies。 – Tome

回答

4

在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-jar-with-dependencies.jar。

+0

此部分应该是' ...'块放入?我尝试将它添加到' ...'将/ etc/hbase/conf目录添加到类路径中,但似乎没有效果。 –

+2

如果你想在exec:exec中有一个依赖项和一个本地路径会怎么样? – sloven

1

类要设置其他类路径中的行家,你应该在如下Maven的配置文件中使用:

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

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

+2

Surefire是用来运行单元测试的插件。这个问题与在运行时使用exec插件添加额外的jar相关,所以修改他的POM将不会有所帮助/工作。在你的链接中“Surefire插件按照以下顺序构建** test **类路径” –

+1

虽然这个答案链接到maven-surefire插件,但它显示了一个如何扩展类路径的正确示例。 exec-maven-plugin支持它,但没有提供示例,请参阅http://www.mojohaus.org/exec-maven-plugin/java-mojo.html#additionalClasspathElements –

2

标准的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中的等价物,无论..