【问题标题】:How to set classpath for mvn exec:exec?如何为 mvn exec:exec 设置类路径?
【发布时间】:2013-09-28 07:28:39
【问题描述】:

我试图让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

这是我的pom.xml 中的&lt;build&gt; 部分:

 <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.sun.speech.freetts.en.us.cmu_us_slt_arctic.ArcticVoiceDirectorycmu_us_slt_arctic.jar 中的一个类

【问题讨论】:

标签: maven classpath exec


【解决方案1】:

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

【讨论】:

  • 这个&lt;plugin&gt;...&lt;/plugin&gt;块应该放在哪个部分?我尝试将其添加到&lt;build&gt;&lt;plugins&gt;... 以将目录/etc/hbase/conf 添加到类路径,但似乎没有效果。
  • 如果你想在 exec:exec 中有一个依赖和一个本地路径,这可能吗?
【解决方案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 等中执行等效操作..

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

    • Surefire 是用于运行单元测试的插件。这个问题与在运行时使用 exec 插件添加额外的 jars 有关,因此按照您的描述修改他的 POM 将无济于事。从您的链接“surefire 插件按以下顺序构建 test 类路径”
    • 虽然此答案链接到 maven-surefire-plugin,但它显示了如何扩展类路径的正确示例。 exec-maven-plugin 支持但不提供示例,见mojohaus.org/exec-maven-plugin/…
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 2012-05-20
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多