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

sim*_*ico 6 exec classpath maven

我正在尝试使用类路径中的本地jar运行mvn exec:exec(或mvn exec:java)运行我的程序.但是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)
Run Code Online (Sandbox Code Playgroud)

使用以下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
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

有人能告诉我,我应该怎么修改pom.xml,这样mvn exec:exec的工作方式类似于java上面的命令?

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

小智 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>
Run Code Online (Sandbox Code Playgroud)

将local.jar替换为需要出现在某个固定位置的所有jar文件(假设项目根目录为pox.xml所在的位置).还要注意使用'project-jar-with-dependencies.jar',在你的情况下它应该是target\XpressAudio-1.0-SN APSHOT-jar-with-dependencies.jar.

  • 如果你想在exec:exec中使用依赖关系和本地路径怎么办? (2认同)