21 Jun 2010

Creating a executable script from a Maven project

If you have a Maven project with a bunch of dependencies, and you've written a harness to launch your thing, it's handy to be able to create a shell script to run your harness with all your dependencies on the classpath.  The Maven assembler plugin is what you need.

In your pom:

<build>
 <plugins>
   ... 
   <plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>appassembler-maven-plugin</artifactId>
   <configuration>
      <programs>
        <program>
           <mainClass>com.example.yourApp.YourClassWithMainMethod</mainClass>
           <name>my_cool_app</name>
        </program>
      </programs>
    </configuration>
   </plugin>
  </plugins>
</build>

Then run:

$ mvn package appassembler:assemble

This produces a shell (and .bat) script in target/appassembler:

$ sh target/bin/my_cool_app
Hello world!

This is one of those "I keep forgetting how to do this so I'd better blog it so I can find it later" kind of posts: It's not original, but handy, and I keep forgetting to run the package part of the Maven command.

23 Jul 2009

Using an existing Scala + Maven project in Eclipse

In case you're having problems using the Scala IDE for Eclipse with Maven projects, here's what I do:
  • Generate the Eclipse project files from the shell: mvn eclipse:eclipse. If you already have Eclipse .project files, you'll have to remove them first or mvn eclipse:clean.
  • In Eclipse, do a File -> Import... and select "General -> Existing Projects into Workspace", navigate to my maven project and select it.
  • This gives you a Scala project, but you'll need to enable Maven: right click on the root of the project in the Package Explorer in Eclipse, select Maven menu and enable.

The plugins I have look like this:

...and it all works very well. The Maven plugin is the m2 one.

Troubleshooting: If that doesn't work first time for you: Project -> Clean... is your friend. If you're still having problems, a Refresh and Close Project followed by an Open project is good. In the worst case, close the project, exit Eclipse, remove the .metadata folder (at your own risk), and restart.

You might also want to check out the Scala IDE for Eclipse wiki pages for how others use Maven.

And for the record, I'm using a Mac (64bit, 10.5.7), with Maven 2.2.0 configured in Eclipse preferences to be an External installation (not the Embedded, but I don't know if that makes a difference or not), JDK 1.5, using Eclipse classic 3.4.2.

Richard Dallaway's Posterous

Director at Spiral Arm Ltd. We build stuff using Scala+Lift, offer consulting & create new projects. I live in Brighton, UK.