【问题标题】:Problem translating java command to maven profile将 java 命令转换为 maven 配置文件时出现问题
【发布时间】:2019-09-27 03:22:47
【问题描述】:

使用mvn 和maven-assembly-plugin,我创建了一个带有依赖项的.jar 并像这样运行它:

java -cp ../target/module-jar-with-dependencies.jar module.Launcher --project=example --network=toy_ags_network.sif

我想创建一个可以做到这一点的 mvn 配置文件。所以在我的pom.xml 我添加了这个:

<profiles>
        <profile>
            <id>runExample</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.6.0</version>
                        <executions>
                            <execution>
                                <phase>compile</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                                <configuration>
                                    <mainClass>module.Launcher</mainClass>
                                    <arguments>
                                        <argument>--project</argument>
                                        <argument>example</argument>
                                        <argument>--network</argument>                                      
                                        <argument>toy_ags_network.sif</argument>                              
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                        <configuration>
                            <mainClass>com.test.Startup</mainClass>
                            <cleanupDaemonThreads>false</cleanupDaemonThreads>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

所以,当我这样做时:mvn compile -P runExample 我会得到相同的结果。似乎依赖项中的某些类没有完全加载或其他东西,这会引发异常等,并且当我不包含使用这些其他类的特定代码时,一切都很好。我想确保以上述方式包含所有依赖项,例如java 命令和 maven 是相等的。

编辑

通过运行mvn exec:exec,我设法拥有了一个与 java 命令行为方式相同的简单插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-cp</argument>
            <argument>target/module-jar-with-dependencies.jar</argument>
            <argument>module.Launcher</argument>
            <argument>--project</argument>
            <argument>example</argument>
            <argument>--network</argument>
            <argument>toy_ags_network.sif</argument>
        </arguments>
    </configuration>
</plugin>

但我想要一个包含该插件的个人资料,这就是我仍然没有的!

【问题讨论】:

  • 我刚刚检查过,它对我有用。如果我添加一些依赖项(例如 junit 使用编译范围而不是测试)并像使用 maven 一样运行它,我会得到(最后几行):[INFO] --- exec-maven-plugin:1.6.0: java (default) @ ploter --- 来自依赖的一些类 (Test.class.toString()): interface org.junit.Test 参数: --project example --network toy_ags_network.sif [INFO] - [INFO] BUILD SUCCESS [INFO] - INFO] 总时间:1.887 s [INFO] 完成时间:2019-05-09T14:02:43+02:00
  • @zolv 它对我也适用于其他依赖项,但不适用于特定依赖项。执行这两个命令的方式必须有所不同(通过 mvn 和 java)或在该依赖项中发生了一些奇怪的事情......
  • 您能否检查一下您错过的依赖项的scope 是什么?是runtime吗?
  • 不,它是编译范围(默认)。我设法运行了一个插件,我会将它包含在问题中。

标签: java maven maven-assembly-plugin exec-maven-plugin


【解决方案1】:

pom.xml 的正确配置是:

<profiles>
        <profile>
            <id>runExample</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.6.0</version>
                        <executions>
                            <execution>
                                <phase>compile</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>java</executable>
                                    <arguments>
                                        <argument>-cp</argument>
                                        <argument>target/module-jar-with-dependencies.jar</argument>
                                        <argument>module.Launcher</argument>
                                        <argument>--project</argument>
                                        <argument>example</argument>
                                        <argument>--network</argument>                                      
                                        <argument>toy_ags_network.sif</argument>                              
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

因此,运行:mvn compile -P runExample 等同于:

java -cp ../target/module-jar-with-dependencies.jar module.Launcher --project=example --network=toy_ags_network.sif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    相关资源
    最近更新 更多