1

How do I export multiple projects as individual jar files each, so as to have a 1:1 ratio of project to jar? I have thousands of projects, and exporting each of them one by one would take way too long, and exporting them all as one jar is also not ideal, because I run them individually via the cron scheduler.

Edit - What I need, to be specific, is exactly what I get from running "Export -> Runnable JAR" in Eclipse, having "Package required libraries into generated JAR", except to do it in a faster way. Maven is apparently good for this, but I'm having trouble getting it to work like I want.

2 Answers2

2

You can use Maven. Then you just have to run a command like mvn package in each project folders to generate the jar files. You can write a script to automate this process.

Benoit
  • 1,995
  • 1
  • 13
  • 18
  • I just gave it a shot. I set up a simple pom.xml, with very simple specifications (aka creating a jar, setting the name, etc.), and it worked with `mvn package`, but it wasn't a runnable jar. I looked up how to make it work, and had no luck so far. Any tips? – Aroldo Bettega Jul 03 '12 at 17:38
  • I'm also having trouble with setting jar dependencies. My projects use Jsoup, and I have to package them all together. I'll edit the question to make it more specific. – Aroldo Bettega Jul 03 '12 at 18:07
  • @AroldoBettegaNetto you can find how to do this with maven [here](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven). – Benoit Jul 03 '12 at 20:56
1

To get the maven to create a runable jar with all your dependencies try doing this:

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>com.whatever.YourMainClass</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.whatever.YourMainClass</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>
Hiro2k
  • 5,254
  • 4
  • 23
  • 28
  • Didn't work as intended. Created two instances of my jar (one called Project1.0.jar, other called Project1.0-jar-with-dependencies.jar). Here's the pom as I have it: ` 4.0.0 group Project 1.0 ` Followed by above post, edited where necessary. – Aroldo Bettega Jul 03 '12 at 18:37
  • Yeah that's the only drawback, since it creates two jars. But the second jar-with-dependencies should do what you asked for. Just try double clicking it. – Hiro2k Jul 03 '12 at 18:45
  • No go, I'm getting a NoClassDefFoundError. Apparently I'm setting the wrong path in the pom, but I don't see how. Currently, it's set to main.Main. The project structure goes like this: Project -> src -> main -> Main.java. Is that wrong? – Aroldo Bettega Jul 03 '12 at 18:52