cl4cds icon indicating copy to clipboard operation
cl4cds copied to clipboard

Add a Maven Build

Open marschall opened this issue 7 years ago • 6 comments

Thank you for your work. I found the project a bit difficult to use because it contains no build script so I created one for Maven.

Building in Eclipse requires M2Eclipse http://www.eclipse.org/m2e/

Building outside of Eclipse requires Apache Maven https://maven.apache.org/install.html

An argument can be made that the package name should be io.simonis.cl4cds instead of io.simonis but I left this unchanged.

marschall avatar Feb 06 '18 18:02 marschall

Hi Philippe,

thanks a lot for you pull request! I'm not working with Maven a lot and I don't really know much about it so I very welcome your contribution.

On the command line, I could successfully use Maven to build the project with your pom file.

However in Eclipse with the m2e plugin I see some problems during the generation of the Javadoc. If the default JDK in Eclipse is set to jdk9, the javadoc tool ca not be found at all:

[INFO] --- maven-javadoc-plugin:3.0.0:jar (attach-javadocs) @ cl4cds ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.056 s
[INFO] Finished at: 2018-02-07T19:08:35+01:00
[INFO] Final Memory: 19M/63M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.0.0:jar (attach-javadocs) on project cl4cds: MavenReportException: Error while generating Javadoc: Unable to find javadoc command: The environment variable JAVA_HOME is not correctly set. -> [Help 1]

If the default JDK in Eclipse is set to jdk8, Maven seems to pick up the javadoc from jdk8 (although it correctly uses javac from jdk 9 to compile java files) which results in the following error:

[INFO] --- maven-javadoc-plugin:3.0.0:jar (attach-javadocs) @ cl4cds ---
[INFO] 
Loading source file /cl4cds/git/src/main/java/module-info.java...
Loading source files for package io.simonis...
5 errors
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.941 s
[INFO] Finished at: 2018-02-07T19:17:45+01:00
[INFO] Final Memory: 20M/288M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.0.0:jar (attach-javadocs) on project cl4cds: MavenReportException: Error while generating Javadoc:
[ERROR] Exit code: 1 - /cl4cds/git/src/main/java/module-info.java:1: error: class, interface, or enum expected
[ERROR] module io.simonis.cl4cds {
[ERROR] ^
[ERROR] /cl4cds/git/src/main/java/io/simonis/cl4cds.java:119: error: <identifier> expected
[ERROR] try (in) {
[ERROR] ^
[ERROR] /cl4cds/git/src/main/java/io/simonis/cl4cds.java:119: error: ')' expected
[ERROR] try (in) {
[ERROR] ^
[ERROR] /cl4cds/git/src/main/java/io/simonis/cl4cds.java:119: error: '{' expected
[ERROR] try (in) {
[ERROR] ^
[ERROR] /cl4cds/git/src/main/java/io/simonis/cl4cds.java:120: error: not a statement
[ERROR] String line;
[ERROR] ^
[ERROR] 
[ERROR] Command line was: /jdk1.8.0_66/jre/../bin/javadoc @options @packages @argfile

I've tried to use the <javadocVersion>9</javadocVersion> property in the pom file, but it doesn't seem to work.

Is there any way to make the javadoc Maven plugin choose toe corect javadoc version (much the same as it chooses the correct javac version)?

I'm using Eclipse Oxygen.2 Release (4.7.2) with m2e 1.8.2.20171007-0217.

simonis avatar Feb 07 '18 18:02 simonis

How are you trying to generate Javadocs in Eclispe, when are you getting the error?

marschall avatar Feb 07 '18 18:02 marschall

I see the reported problems when I use “Run as”->m2e->install (or package) from the project context menu. How else would build the project from within Eclipse using Maven?

Maybe the easiest solution would be to completely remove the javadoc step from the Maven file. The project is for a command line utility so I don’t expect it will ever contain a substantial API.

Also, would it make sense to add a default target to the pom file? I don’t know what's common in the Maven world.

simonis avatar Feb 08 '18 11:02 simonis

There isn't really a need to explicitly run Maven in Eclipse with m2e. m2e will set up everything including class path containers so that the normal Eclipse incremental build uses Maven. Simply save your files and you're done. You just have to make sure your project has the maven2 nature and builder.

You can verify your project has the maven2 nature and builder when it has a blue "M" decorator on the top left (or looking in the .project file if this is your thing).

Usually the maven2 nature and builder get added using one of these two ways:

Configure → Convert to Maven Project File → Import → Existing Maven Projects

Maven offers predefined phases which are similar to goals. A complete list can be found at Lifecycle Reference.

The most important ones are

  • clean
  • compile
  • test
  • package

To be clear, I believe the project should have an automated build (the ability to build on the command line without having to use a specific IDE or directly invoking javac). I don't particularly care what is used (Make, Ant, Gradle, Maven, Shell Script, …). I'm most familiar with Maven so I used Maven. I'm happy to answer any Maven related questions, including beginner ones.

marschall avatar Feb 08 '18 12:02 marschall

I fully agree that the project should have an automated build and I'm really thankful that you've created one! I'm just trying to understand how this is working which shouldn't be any criticism on your submitted code but just proof of my ignorance :) As I wrote, I could easily use your pom-file with Maven on the command line. Simply calling mvn on the command line doesn't work because the pom file contains no default target (that's why I asked you if it is possible to add one). But I don't know what's the Maven convention. If it is normal to have no dfault goal, that's fine for me. So I called mvn package which worked fine and created:

$ ls -l target
drwxr-xr-x 5 xxx kmem   4096 Feb  8 20:06 apidocs
-rw-r--r-- 1 xxx kmem  10364 Feb  8 20:06 cl4cds-1.0.0-SNAPSHOT.jar
-rw-r--r-- 1 xxx kmem 373145 Feb  8 20:06 cl4cds-1.0.0-SNAPSHOT-javadoc.jar
-rw-r--r-- 1 xxx kmem   6265 Feb  8 20:06 cl4cds-1.0.0-SNAPSHOT-sources.jar
drwxr-xr-x 3 xxx kmem   4096 Feb  8 20:06 classes
drwxr-xr-x 2 xxx kmem   4096 Feb  8 20:06 javadoc-bundle-options
drwxr-xr-x 2 xxx kmem   4096 Feb  8 20:06 maven-archiver
drwxr-xr-x 3 xxx kmem   4096 Feb  8 20:06 maven-status

Now when I import the Maven project into Eclipse (exactly as you described) only the "compile" target seems to be called by default, because I get the following:

$ ls -l target
drwxr-xr-x 3 xxx kmem 4096 Feb  8 19:54 classes
drwxr-xr-x 2 xxx kmem 4096 Feb  8 19:54 test-classes

I thought it would be nice to generate the jar file as well, so I called "Project->Run As->Maven build... " and entered "packages" as goal. This gave me the problems I've described in my first mail.

If that's the normal behaviour that the Maven plugin in Eclipse can only be used to compile a Maven project but not to execute any other goals, that's a little disappointing, but I can live with that. I just wanted to ask if that's the expected behaviour or if there's another way of executing the various Maven goals from within Eclipse.

If you think this is a problem of the Eclipse Maven plugin, that's OK either.

Thanks, Volker

simonis avatar Feb 08 '18 19:02 simonis

Simply calling mvn on the command line doesn't work because the pom file contains no default target (that's why I asked you if it is possible to add one). But I don't know what's the Maven convention. If it is normal to have no dfault goal, that's fine for me.

To the best of my knowledge Maven does not have a default phase. I have never seen a project define one and don't even know if that's possible.

so I called "Project->Run As->Maven build... " and entered "packages" as goal

That looks like a bug in m2e. First I thought that the issue was that under the "JRE" tab it defaults to "Workspace default JRE" rather than an "Execution Environment". But setting this to "JavaSE-9" did not fix the issue. Only defining JAVA_HOME to a JDK 9 folder in the "Environment" tab solved the issue for me.

marschall avatar Feb 13 '18 11:02 marschall