This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
preface
See this question, in fact, exhausted their brains, as if did not think of any scenario would use this. Little brother knowledge is not home!!
However, attention is still quite high, after all, there are special solutions, then there will be a good scene to wait!
We write it down, so that we can use it someday.
Problem of repetition
The question is simple:
Using Maven to build a simple Java application, want to use Maven command line, to complete the implementation of the Java application Main method.
Similar to:
mvn run-app com.example.MainClass
Problem solving
The solution to this problem is actually quite simple, using Maven plug-ins to complete the task.
Maven provides a plugin, maven-exec-plugin, which can take on the task of executing the Main method.
So let’s see how it works.
-
First, ensure your Jdk environment, which I won’t go into, because since writing Java programs, JVM environment is necessary.
-
Then, modify the POM.xml file
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
Copy the code
<properties>
<exec.mainClass>Class full path name</exec.mainClass>
</properties>
Copy the code
- Execute the command
mvn clean compile exec:java
You can add running parameters by using -dexec. args=” XXX”
If you really don’t want to add a plug-in, you can
MVN org. Codehaus. Mojo: exec -- maven plugin: 1.5.0: Java - Dexec. MainClass = "com. Example. Main" | grep - Ev '(^ \ [| Download \ w + :)'
The problem summary
In fact, technology is endless, do not know always on the road. Keep learning, is king.
PS: Tail promotion, welcome friends to pay attention to my public number. Public number, long-term update of the original, classic reprint technology, structure knowledge