如何为mvn exec:exec设置类路径? [英] How to set classpath for mvn exec:exec?

查看:99
本文介绍了如何为mvn exec:exec设置类路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让mvn exec:exec(或mvn exec:java)在类路径中使用本地jar运行我的程序.但是jar无法加载:

I'm trying to have mvn exec:exec (or mvn exec:java) run my program with a local jar in the classpath. However the jar fails to load:

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运行程序的工作原理:

Running the program directly from the CLI using java works:

    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命令一样工作吗?

Could someone tell me how should I edit the pom.xml such that mvn exec:exec works like the java command above?

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

推荐答案

在maven中,可以使用systemPath包含本地jar(在maven存储库外部).但是,由于范围是系统的(对于使用systemPath声明的依赖项),因此几乎没有限制,因此它仅适用于exec:java.

In maven it is possible to include a local jar (which is outside of maven repository) using systemPath. But since the scope is system (for dependencies declared with systemPath), there are few limitations and because of that it only works with exec:java.

对于exec:exec,以上解决方案将不起作用,因为maven在其生成的(运行时)类路径(%classpath)中不包含系统范围的依赖项,因此解决方案是使用您自己的类路径,而不是maven生成的类路径,如下所示

For exec:exec, the above solution will not work because maven does not include system scoped dependencies in its generated (runtime) classpath (%classpath) so the solution is to use your own classpath instead of maven generated classpath as shown below.

        <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.

Replace local.jar with all the jar files that are required to be present at some fixed location (here project root is assumed, where pox.xml is located). Also note the use of 'project-jar-with-dependencies.jar', in your case it should be target\XpressAudio-1.0-SN APSHOT-jar-with-dependencies.jar.

这篇关于如何为mvn exec:exec设置类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆