2013-09-28 93 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中的等價物,無論..