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

标签 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>我的一部分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工作原理类似于 java上面的命令?

com.sun.speech.freetts.en.us.cmu_us_slt_arctic.ArcticVoiceDirectorycmu_us_slt_arctic.jar中的一个类

最佳答案

在 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-dependency.jar”的使用,在您的情况下,它应该是 target\XpressAudio-1.0-SN APSHOT-jar-with-dependency.jar。

关于maven - 如何设置 mvn exec :exec? 的类路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19064535/

相关文章:

maven - NiFi 构建错误

java - 使用什么 wtpversion 值?

maven - 我可以在运行时指定一个 Maven project.artifactId 吗?

spring - 如何访问 JAR 文件中的属性?

bash - 了解 { }/dev/null\;将 find 与 -exec 一起使用时

go - exec: 在 $PATH 中找不到可执行文件

java - QueryParam 在 Swagger API 中声明为不需要

java - 如何在 Eclipse 中指定测试类路径

maven - 使用 Maven 在 list 的类路径中生成非版本化的 jar 条目

python - 从 Python 中的字符串列表处理函数