Invoke a Maven artifact from a Bash script, passing arbitrary arguments?

255 views Asked by At

The aim is to have a Bash script that runs a Java program from a project that is built with Maven, ideally with a classpath that uses all the JARs needed from the Maven cache.

I can think of two solutions for this, but they are all a bit ugly or cannot deal with certain problems:

1) use the exec:java plugin/goal. The problem I could not solve there is that one cannot pass arbitrary arguments to the program from the command line with this solution: the only way to pass arguments is by setting the value of a mvn command line argument. So if there are several arguments, that value needs to be quoted. However if the arguments to pass to the program need quoting and e.g. option values can contain spaces, there seems to be no way to do this.

2) use the dependency plugin to create a file that contains the classpath using the build-classpath goal. This is brilliant to generate a file that contains the locations of all JARs in the Maven cache, which the project depends on. However, it seems to be impossible to also add the location of the project artifact JAR to that file. So the content from this file needs to get combined with the location of the project JAR somehow:

2.1) use mvn package and use the JAR in target directory. This is ugly because ideally we would also want to use the JAR from the Maven cache, like for the dependencies

2.2) have another dummy Maven project that depends on the one which I want to run and use the build-classpath goal there. This will then give me the full classpath, but at the cost of needing to have another project and run the necessary goals in that project. If the project is in a subdirectory of the original project, I can also see no nice way of how to build that fake project as part of the main project except maybe using the exec:exec plugin/goal, which is ugly.

Is there anything I am missing or a recommended way of how to do this in a more straightforward way?

UPDATE: I was asked in the comments why I want to use the Maven cache in the first place. The reason is that in this context, we know that everything needed is already in the maven cache, we are talking about rather large dependency chains and it would feel very awkward and clumsy to use a different mechanism and different cache, just in order to make this work. It is hard to understand why Maven does not provide an easier solution to this right away.

0

There are 0 answers