0

I would like to generate a runnable JAR for my Eclipse Java projects. The JAR should contain all my code, with unmodified namespaces, classes, resources etc. My workspace is organized into a multitude of different projects.

When I right-click a project node in Project Explorer, and select "Run As > Maven install", I get the following:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< foo.Project1:foo.Project1 >----------------------
[INFO] Building foo.Project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for foo.Project2:foo.Project2:jar:0.0.1-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.220 s
[INFO] Finished at: 2020-06-26T12:20:11+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project foo.Project1: Could not resolve dependencies for project foo.Project1:foo.Project1:jar:0.0.1-SNAPSHOT: Could not find artifact foo.Project2:foo.Project2:jar:0.0.1-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

foo.Project1 runs fine under Eclipse. Its pom.xml file is:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>foo.Project1</groupId>
  <artifactId>foo.Project1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>foo.Project2</groupId>
        <artifactId>foo.Project2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

(A ZIP of the two projects can be accessed here: https://wetransfer.com/downloads/b9bb4aa840daa6e7b2dd2c793acdb56420200626103047/32c0fd)

I tried the first solution suggested in How can I create an executable JAR with dependencies using Maven? (with 2300+ score) by:

1: adding the following in the build section of the pom.xml:

  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>foo.Project1.Application</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>

2: right-clicking the project node, then "Run As > Maven build..."

3: defining "Goals" as: clean compile assembly:single, and hitting Run

Same errors.

What am I doing wrong?

Martin Frank
  • 199
  • 1
  • 13

1 Answers1

0

If you want to use foo.Project2 as dependency, you need to build it first with clean install.

You always need to build a project before you can use it as dependency in the Maven build.

Eclipse cheats a bit by also allowing you to reference projects from the workspace and therefore allows you to run the program. But for the "real build", you need to build everything in the right order.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Thanks! However I have lots of projects and can't `clean install` them in dependency order - no way. How can I do this automatically? Further, the generated Project1.jar does not contain everything (Project1+Project2 classes) as I asked. – Martin Frank Jun 26 '20 at 12:03
  • AFAIK, there is no way to do this automatically. If these projects are always developed and built together, you can construct a multi-module-project of them. For that, you put them in the same svn or git repository and give them a standard structure. Then you can build them all at once. – J Fabian Meier Jun 26 '20 at 14:02
  • Your second problem may be a misconfiguration of the assembly plugin. – J Fabian Meier Jun 26 '20 at 14:03
  • I fixed the second problem, which was a mistake from me. As for the main problem, I can't generate an explicit list of 's for every workspace project I have. I expect the build system to handle that for me. I think this is legitimate. – Martin Frank Jun 26 '20 at 14:30
  • I am working with Maven for some years now and I don't know any system that does this automatically. – J Fabian Meier Jun 26 '20 at 14:43
  • Why can't you make an explicit list of modules? I should not take you more than one or two hours to write all that down. – J Fabian Meier Jun 26 '20 at 14:45
  • Eclipse "Export as runnable JAR" does have this kind of system, and it works. Deployment cannot be automated though, eg. from the command line. I would definitely expect Maven to provide something on par with Eclipse built-in packaging capabilities. Actually I find Maven very, VERY weak here... – Martin Frank Jun 26 '20 at 15:45
  • You know that people don't usually build jars (except for testing) in Eclipse? They take Jenkins or some other build server and run Maven in it. There, you don't have a "workspace". – J Fabian Meier Jun 26 '20 at 16:18
  • I can use Maven on the command line, not a problem, if you tell me a reliable method for JAR file generation. Note: even if I used your current method, I would have to execute ìnstall`**in the correct order**, for each project, right? – Martin Frank Jun 26 '20 at 16:27
  • If have 10 separate projects, you need to build them manually in the right order. If you move your 10 projects to *modules* in a multi-module project (so a subdirectory for each, and a main pom listing the modules), then you only need to call `mvn clean install` once on the whole project because inside multi-module projects maven figures out the correct build order itself. – J Fabian Meier Jun 26 '20 at 16:48
  • Thank you. If I follow this route, I guess I will just have to generate the multi-module Maven project programmatically. I guess this is doable. – Martin Frank Jul 02 '20 at 08:38