To run a traditional java program we have to start a main class, preferably in a JAR.
However, these jars usually have dependencies, which need to be on the classpath.
This leads us to two options:
1) we create a 'fat' jar, in which all are included (see one-jar, which I talked about here)
2) when we are allowed to use maven, we can let maven create the classpath for us.
In this post, I'll discuss option 2.
With the maven exec:java plugin (documented here), we can execute a main class from a specific jar.
Maven then builds the classpath!
For an example, we assume we have a main class in my.personal.projects.MainClass, which is hosted in the my.personal.projects.coolapp-1.0.jar, which is in the repository.
It might have any number of dependencies.
A sample pom.xml looks like the following:
<dependency>
<groupId>my.personal.projects</groupId>
<artifactId>coolapp</artifactId>
<version>1.0</version>
</dependency>
...
<mainClass>my.personal.projects.MainClass</mainClass>
<commandLineArgs>${arg1} -myparam ${arg2}</commandLineArgs>
From the command line, we can then execute:
mvn exec:java -Darg1=value1 -Darg2=value2
This then executes the mainclass with the parameters, and ensures that the mainclass and all it's dependencies are on the classpath.
Presto.
However, these jars usually have dependencies, which need to be on the classpath.
This leads us to two options:
1) we create a 'fat' jar, in which all are included (see one-jar, which I talked about here)
2) when we are allowed to use maven, we can let maven create the classpath for us.
In this post, I'll discuss option 2.
With the maven exec:java plugin (documented here), we can execute a main class from a specific jar.
Maven then builds the classpath!
For an example, we assume we have a main class in my.personal.projects.MainClass, which is hosted in the my.personal.projects.coolapp-1.0.jar, which is in the repository.
It might have any number of dependencies.
A sample pom.xml looks like the following:
<dependency>
<groupId>my.personal.projects</groupId>
<artifactId>coolapp</artifactId>
<version>1.0</version>
</dependency>
...
<mainClass>my.personal.projects.MainClass</mainClass>
<commandLineArgs>${arg1} -myparam ${arg2}</commandLineArgs>
From the command line, we can then execute:
mvn exec:java -Darg1=value1 -Darg2=value2
This then executes the mainclass with the parameters, and ensures that the mainclass and all it's dependencies are on the classpath.
Presto.
Reacties
Een reactie posten