theonlygusti theonlygusti - 5 months ago 35
Java Question

How to install with maven?

I am working on a Java project, following this tutorial for bukkit plugins.

In eclipse, when I navigate through Run As > 7 Maven install (as in the below picture) everything works fine and a .jar is created as expected.



However,when I run

mvn install
from the command line it doesn't work, it throws these errors:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project WizardServer: Compilation failure
[ERROR] An unknown compilation problem occurred
[ERROR] -> [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/MojoFailureException


Why can I not build this project using maven at the command line? Does eclipse use a different command to just
mvn install
?






If it is helpful, I put my source into this github repository so you can browse the file tree too.

I have included what I believe are important files below though:


pom.xml


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.theonlygusti</groupId>
<artifactId>WizardServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.7.9-R0.2</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
</project>



WizardServer.java


package io.github.theonlygusti.wizardserver;

import org.bukkit.plugin.java.JavaPlugin;

public final class WizardServer extends JavaPlugin {
@Override
public void onEnable(){
getLogger().info("Enabled!");
}

@Override
public void onDisable(){
getLogger.info("Disabled :(");
}
}


And here is what
mvn install -X
tells me my system is:

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_91, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.4", arch: "x86_64", family: "mac"


See also this pastebin for the full output of
mvn clean install -X
.

Which is pretty correct as far as I can tell.

Answer

Eclipse can indeed use a different process to build your Maven project. Eclipse relies on the M2Eclipse plugin that integrates it to Maven and, by default, uses an embedded Maven installation, which is 3.3.9 for Eclipse Mars.

To fix your command line issue

First of all, your POM declares

<configuration>
  <source>1.8</source>
  <source>1.8</source>
</configuration>

Having twice the <source> element with the same value isn't that much of a problem. Maven will disregard one of them. However, it means that <target> will default to 1.5. And you can't compile a 1.8 source file using 1.8 specific features to a 1.5 byte-code.

As such, you need to declare both <source> and <target> as such:

<configuration>
  <source>1.8</source>
  <target>1.8</target>
</configuration>

With that change, you will have another slight error, because the method onDisable isn't calling the getLogger() method properly:

public void onDisable(){
  getLogger().info("Disabled :(");
           ^^
}

To fix what seems to compile with Eclipse

Running "Run as... > Maven install" in Eclipse does not clean the previous builder folder. It is entirely possible that Maven is relying on previous built data and, thus, not detecting properly the non-compiling code. In such situation, I always recommend to clean before installing, i.e. running mvn clean install. From Eclipse, you can do that by:

  1. Doing "Run as... > Maven clean" then doing "Run as... > Maven install"
  2. Creating a special "Run Configuration" for a Maven build, that invokes the goals "clean install". This way, you can run that configuration without launching two distinct builds.