用 exec.mainClass 覆盖 exec-maven-plugin
Overriding exec-maven-plugin with exec.mainClass
我已经启动了一个新的maven项目并配置了exec-maven-plugin
来设置一个默认的mainClass
,我可以使用
来执行
mvn exec:java -q
Hello World!
但是,如果我尝试在命令行上指定不同的 mainClass,这似乎会被忽略:
mvn exec:java -Dexec.mainClass="com.jamesmcguigan.kdt.App2" -q
Hello World!
如果我从 pom.xml 中注释掉 exec-maven-plugin
部分,上面的行就可以工作,但是最好在 pom.xml
中为 maven exec:java
设置默认值并有办法在 mvn exec:java -Dexec.mainClass="com.jamesmcguigan.kdt.App2"
.
的 cli 上覆盖它
还是我漏掉了什么?
文件
pom.xml
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.jamesmcguigan.kdt.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/jamesmcguigan/kdt/App.java
package com.jamesmcguigan.kdt;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
src/main/java/com/jamesmcguigan/kdt/App2.java
package com.jamesmcguigan.kdt;
public class App2
{
public static void main( String[] args )
{
System.out.println( "Hello World 2!" );
}
}
使用属性,即:
<properties>
...
<main.class>com.jamesmcguigan.kdt.App</main.class>
</properties>
...
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
然后:
mvn exec:java -Dmain.class="com.jamesmcguigan.kdt.App2" -q
要覆盖 main class 和 args,例如:
<project>
...
<properties>
<!-- default values -->
<mainClass>${project.groupId}.${project.artifactId}.App</mainClass>
<mainArgs/>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>${mainClass}</mainClass>
<commandlineArgs>${mainArgs}</commandlineArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
要执行覆盖:
mvn compile exec:java -DmainClass=org.example.Main -DmainArgs="arg1 arg2"
我已经启动了一个新的maven项目并配置了exec-maven-plugin
来设置一个默认的mainClass
,我可以使用
mvn exec:java -q
Hello World!
但是,如果我尝试在命令行上指定不同的 mainClass,这似乎会被忽略:
mvn exec:java -Dexec.mainClass="com.jamesmcguigan.kdt.App2" -q
Hello World!
如果我从 pom.xml 中注释掉 exec-maven-plugin
部分,上面的行就可以工作,但是最好在 pom.xml
中为 maven exec:java
设置默认值并有办法在 mvn exec:java -Dexec.mainClass="com.jamesmcguigan.kdt.App2"
.
还是我漏掉了什么?
文件
pom.xml
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.jamesmcguigan.kdt.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/jamesmcguigan/kdt/App.java
package com.jamesmcguigan.kdt;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
src/main/java/com/jamesmcguigan/kdt/App2.java
package com.jamesmcguigan.kdt;
public class App2
{
public static void main( String[] args )
{
System.out.println( "Hello World 2!" );
}
}
使用属性,即:
<properties>
...
<main.class>com.jamesmcguigan.kdt.App</main.class>
</properties>
...
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
然后:
mvn exec:java -Dmain.class="com.jamesmcguigan.kdt.App2" -q
要覆盖 main class 和 args,例如:
<project>
...
<properties>
<!-- default values -->
<mainClass>${project.groupId}.${project.artifactId}.App</mainClass>
<mainArgs/>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>${mainClass}</mainClass>
<commandlineArgs>${mainArgs}</commandlineArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
要执行覆盖:
mvn compile exec:java -DmainClass=org.example.Main -DmainArgs="arg1 arg2"