2

I have several java non-maven projects (with one main project) and several maven projects (with one main project). Now I need to use the maven-projects functionalities in the non-maven projects.

I have to say, I know very little about maven. And I'm working in NetBeans IDE.

There are several options I've come up with:

  1. Make non-maven projects maven projects and add dependencies. I cannot do that because others use the non-maven projects their way and I cannot just make changes like this.

  2. Make maven projects non-maven projects and add them as Libraries I cannot do that because there would be a lot of libraries to add. The dependencies might be large.

  3. To non-maven projects add jars as libraries of the maven projects. This is same as the (2) option. I tried it and added all the maven project jars as libraries to my non-maven main project, but at a run-time there was a lot of NoClassDefFoundError exceptions because of missing jars (3rd party jars that the maven projects depends on).

  4. ?

Any ideas?

Thank you in advance.


Solved

I used the pom adjustments from chresse. The whole pom is here: https://codeshare.io/5ZeYN2

I used the maven command from the question Tunaki marked this was duplicate of (how-can-i-create-an-executable-jar-with-dependencies-using-maven) mvn clean compile assembly:single

Thank you all.

Community
  • 1
  • 1
Traveler
  • 143
  • 1
  • 2
  • 14
  • You can generate jar file from maven project, and then apply jar from maven project in to non-maven project as library. – MatWdo Jan 13 '17 at 12:25
  • @MateuszW90 Yes, but I have to add also the jars that the maven projects depend on, every single dependency. And also dependency of dependecy. And so on. Am I right? – Traveler Jan 13 '17 at 12:29
  • 4. same as 3, but add all the missing dependencies. That's what Maven would do. – JB Nizet Jan 13 '17 at 12:29
  • @JBNizet I see. I guess it is the only way. Is there any tool that would to that for me? Because it is nearly impossible to check all the dependencies.. (and the dependencies of the dependencies..) – Traveler Jan 13 '17 at 12:33

1 Answers1

3

you can generate a jar from your maven project including all dependencies.

add the following plugin to your pom of your main maven project. the mvn assembly:single will create a additional jar ('projekt'-jar-with-dependencies.jar) with all dependencies, which can be included in your non-maven project:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>
chresse
  • 5,486
  • 3
  • 30
  • 47
  • Thank you. Do I have to set up something? I just added your code between .. into projects pom but without any effect.. The jar with dependencies was not created. – Traveler Jan 13 '17 at 12:43
  • @Traveler: i updated my code so it can be run in the package phase. use the new code and run a `mvn package` – chresse Jan 13 '17 at 13:10
  • Still no success.. This is my pom: https://codeshare.io/5ZeYN2 – Traveler Jan 13 '17 at 13:32
  • It's working now. Thank you for the pom adjustments. As a command I used the `mvn clean compile assembly:single` from [link](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Traveler Jan 13 '17 at 13:47