7

我试图让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>我的部分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:execjava命令一样工作的东西吗?

com.sun.speech.freetts.en.us.cmu_us_slt_arctic.ArcticVoiceDirectory是一个类cmu_us_slt_arctic.jar

4

3 回答 3

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

于 2013-12-27T05:52:15.110 回答
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 等中执行等效操作。

于 2013-12-27T01:49:41.887 回答
1

要在 maven 中设置额外的类路径,您应该在 maven 配置文件中使用如下:

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

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

于 2013-10-05T15:59:26.823 回答