Search code examples
javamavenprogram-entry-pointartifactexec-maven-plugin

Running main classes from a deployed artifact with maven


I don't get it. I've set up my pom.xml to use the Maven exec plugin so I can execute some of the classes in my project with the correct classpath, -D defines and -javaagent. So from a shell with the classes built in ./target/classes etc.. I can run the main() methods using

mvn exec:java -Dexec:mainClass=classWithAMainMethod

All good so far.

Now I want to ship my project(a jar artifact) and I still want to be able to use the configuration I've put in the pom.xml for running the classes with the correct arguments etc.. How do I do it? Is there some way of staying

mvn -artifactJar=MyArtifact.jar exec:java -Dexec:mainClass=classWithAMainMethod

when all I have is MyArtifact.jar(Or a maven repository with MyArtifact.jar in it)??

I've tried the following:

  • Get the jar with the dependency:get goal and unzip it. I can't do anything with it as the pom.xml seems to end up in META-INF/maven in the artifact jar. Is there any way of using it?

  • Creating a dummy pom where I want to run my project with a single dependency on my projects artifact. I can then use exec:java to run the main classes but it's dosen't uses the configuration from my projects pom.

Thanks.


Solution

  • The AppAssembler plugin worked out quite well for me. I replaced the exec plugin config in my project's pom with something like this in the build section:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>appassembler-maven-plugin</artifactId>
        <version>1.2.2</version>
        <configuration>
            <repositoryLayout>flat</repositoryLayout>
            <repositoryName>lib</repositoryName>
            <extraJvmArguments>
                -Djava.rmi.server.hostname=localhost
                -javaagent:${spring.javaagent.jar}
            </extraJvmArguments>
            <programs>
                <program>
                    <name>foo1</name>
                    <mainClass>net.foor.FooMain</mainClass>
                </program>
                   ...
        </configuration>
    </plugin>
    

    In Eclipse I created an external tools launcher to run the resulting scripts from target/appassembler/bin

    On the machine I wanted to deploy to(Assuming access to the internal Maven repository where my artifact+dependencies have been installed/deployed):

    • First use wget or mvn dependency:get to get a copy of my artifact jar.
    • Extract the pom. unzip -j artifact.jar */pom.xml*
    • Run mvn appassembler:assemble -DassembleDirectory=.
    • Move the artifact.jar into the ./lib directory
    • Set execute permissions on generated shell scripts in ./bin