Tim P
Tim P

Reputation: 1008

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:

Thanks.

Upvotes: 2

Views: 2755

Answers (2)

Tim P
Tim P

Reputation: 1008

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

Upvotes: 2

bvulaj
bvulaj

Reputation: 5123

Have you tried using something like onejar?

That sounds like what you're looking for.

Upvotes: 1

Related Questions