我创建了一个使用Maven构建的简单控制台Java应用程序.是否有一种方法可以使用maven命令从命令行运行主类(不需要任何参数),如:
mvn run-app com.example.MainClass
Run Code Online (Sandbox Code Playgroud) 我有一个模拟服务器,我需要在运行自动化测试之前在 Jenkins 中启动它,然后在测试运行后停止模拟服务器。
这个模拟服务器是一个 Maven 项目,并且正在使用exec-maven-plugin. 我可以通过运行命令来启动该服务器mvn exec:java。但是我无法通过 Jenkins 停止这个 Maven 项目。我读过相关内容,大多数答案都告诉我们找到这个 Maven 项目的进程 ID,然后杀死它。有没有更干净、更简单的方法来阻止这个项目?
在 pom.xml 中,有一种用法是maven-dependency-plugin将特定的外部 JAR 文件下载到单独的位置(在 /tmp/externalTestJars/testjar.jar 中)。
我想用来exec-maven-plugin在testjar.jar文件(Main.java)中运行一个 java 类。
我发现这个 SO question提出了同样的问题,但该问题的答案对我没有帮助。
如果我直接运行 Main.java 文件(在创建 .jar 的原始项目中,使用mvn exec:java),我可以使用以下 pom 配置。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>org.example.Main</mainClass>
<!-- need to pass two arguments to the Main.java file main method -->
<arguments>
<argument>arg one</argument>
<argument>arg two</argument>
</arguments>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
在上面的 SO 问题中,它有一个类似下面的答案来在 .jar 文件中运行一个 java 文件。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>org.example.Main</mainClass>
<arguments>
<argument>-jar</argument>
<argument>/tmp/externalTestJars/testjar.jar</argument>
</arguments>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
但在我的情况下,这些参数将被视为传递给 Main.java 中的 …
使用以下命令可以轻松编译 Java 源代码--enable-preview:
<!-- Enable preview features -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>15</release>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
但你怎么能跑呢exec:java?使用
<!-- Exec plugin.. run with `mvn exec:java` -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>${mainClass}</mainClass>
<commandlineArgs>--enable-preview</commandlineArgs>
<arguments>
<argument>--enable-preview</argument>
</arguments>
</systemProperties>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
仍然会出现以下错误:
An exception occured while executing the Java class.
Preview features are not enabled for Main (class file version 59.65535).
Try running with '--enable-preview'
Run Code Online (Sandbox Code Playgroud) 尽管 Maven exec 插件添加的执行之一出现错误,如何才能使 Maven 构建继续进行?
我在使用 Maven Exec 插件时遇到了一些困难。我想使用该插件执行两个单独的mvn命令,因此我只需要执行mvn exec:exec即可执行所有命令。
以下两个命令单独运行效果良好:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>clean</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>package</argument>
</arguments>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
这是我尝试将两者结合起来以便用一个命令执行它们:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>id1</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>clean</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>id2</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>package</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我现在执行mvn exec:exec时,我收到以下错误消息:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder …Run Code Online (Sandbox Code Playgroud)