2

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.

Tim P
  • 948
  • 1
  • 12
  • 19
  • Maybe I should be using the [AppAssembler](http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/index.html) plugin instead.. – Tim P May 18 '12 at 16:56

2 Answers2

2

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
Tim P
  • 948
  • 1
  • 12
  • 19
1

Have you tried using something like onejar?

That sounds like what you're looking for.

bvulaj
  • 5,023
  • 5
  • 31
  • 45
  • What does that give me (other than shading) that the assembly plugin doesn't? Does it 'preserve' the pom.xml file in some way? The problem is not about the dependencies. It's the plugin configuration for the exec plugin I would like to keep. Thanks – Tim P May 18 '12 at 14:44
  • This is a better suggestion that what you think. The point here is that with all the dependencies packed into a single JAR, you can just use the java command line to run your main. Just fetch the one JAR from your repo and invoke your main...2 simple commands. – Dave Nov 07 '17 at 15:00