Maven
- Example-driven introduction to Maven, "Maven by Example": http://books.sonatype.com/mvnex-book/reference/public-book.html
- Reference book: http://books.sonatype.com/mvnref-book/reference/
- Conceptual model: http://warren.chinalle.com/wp-content/uploads/2011/07/maven-concepts1.png
If you use Maven with its default settings, chances are that you will get an error message like these:
[...] not supported in -source 1.3
[...] not supported in -source 1.5
To use Java 1.7, add the following element to the pom.xml
file:
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
To use Java 1.8, add the following element to the pom.xml
file:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
In you develop in Eclipse and use the m2e plug-in, you can update the project's .classpath
file by right clicking the project and choosing Maven | Update Project....
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Add the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>fully.qualified.name.of.main</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
The following part adds the lib
directory to the classpath:
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
If you intend to use dependencies (and you do, that's the main advantage of Maven), you have to copy them to the lib
directory. To do so, add the following to the <plugins>
element:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
First, you should start by creating an Xtend archetype: https://www.eclipse.org/xtend/download.html#MavenSupport
If the Tycho compiler fails with the following error after the xtend maven builder completes successfully:
[ERROR] Failed to execute goal org.eclipse.tycho:tycho-compiler-plugin:0.18.1:compile (default-compile) on project X: Compilation failure: Compilation failure:
[ERROR] MyClass.java:[X,0]
[ERROR] MyXtendClass _var = new MyXtendClass();
[ERROR] ^^^^^^^^^^^^
[ERROR] MyXtendClass cannot be resolved to a type
Check the following:
- The character encoding is correct (
UTF-8
), and opening the generated Java code inxtend-gen
seems okay. - The
xtend-gen/
directory is added to the list of source folders inbuild.properties
. - This is normally a warning, but sometimes Eclipse does not display it correctly.
- Tycho looks for sources based on this configuration. Hence, as a general rule of thumb, warnings in the
build.properties
file should be treated as an error.
[ERROR] Internal error: org.eclipse.tycho.core.osgitools.OsgiManifestParserException:
Exception parsing OSGi MANIFEST /home/szarnyasg/.m2/repository/p2/osgi/bundle/org.eclipse.core.resources/3.8.1.v20121114-124432/org.eclipse.core.resources-3.8.1.v20121114-124432.jar:
error in opening zip file -> [Help 1]
org.apache.maven.InternalErrorException: Internal error: org.eclipse.tycho.core.osgitools.OsgiManifestParserException: Exception parsing OSGi MANIFEST /home/szarnyasg/.m2/repository/p2/osgi/bundle/org.eclipse.core.resources/3.8.1.v20121114-124432/org.eclipse.core.resources-3.8.1.v20121114-124432.jar: error in opening zip file
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:168)
[...]
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.eclipse.tycho.core.osgitools.OsgiManifestParserException: Exception parsing OSGi MANIFEST /home/szarnyasg/.m2/repository/p2/osgi/bundle/org.eclipse.core.resources/3.8.1.v20121114-124432/org.eclipse.core.resources-3.8.1.v20121114-124432.jar: error in opening zip file
at org.eclipse.tycho.core.osgitools.DefaultBundleReader.doLoadManifest(DefaultBundleReader.java:74)
[...]
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
... 11 more
Caused by: java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
[...]
at org.eclipse.tycho.core.osgitools.DefaultBundleReader.doLoadManifest(DefaultBundleReader.java:67)
... 21 more
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.0:shade (make-shade) on project [...]:
Error creating shaded jar: Unexpected end of ZLIB input stream -> [Help 1]
[...]
Caused by: java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:411)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.codehaus.plexus.util.IOUtil.copy(IOUtil.java:188)
at org.codehaus.plexus.util.IOUtil.copy(IOUtil.java:174)
at org.apache.maven.plugins.shade.DefaultShader.addRemappedClass(DefaultShader.java:259)
at org.apache.maven.plugins.shade.DefaultShader.shade(DefaultShader.java:151)
at org.apache.maven.plugins.shade.mojo.ShadeMojo.execute(ShadeMojo.java:484)
... 21 more
Possible cause: some files were downloaded with errors. Deleting the affected files or the whole local repository (the ~/.m2
directory) solves the problem.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
The org.apache.maven.plugins:maven-source-plugin
does not work the org.eclipse.tycho:tycho-maven-plugin
: it results in No sources in project. Archive not created.
and No product definitions found. Nothing to do.
erros. Instead of the maven-source-plugin
, use the following configuration:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-source-plugin</artifactId>
<version>${tycho.version}</version>
<executions>
<execution>
<id>plugin-source</id>
<goals>
<goal>plugin-source</goal>
</goals>
</execution>
</executions>
</plugin>
You may get a NoClassDefFoundError
caused by a ClassDefNotFoundError
for classes in Maven artifact with a SNAPSHOT
version. First, check the MANIFEST.MF
file in the generated JAR file. In this case, it contains the following reference: lib/org.eclipselabs.emfjson-0.7.0-20140221.135604-5.jar
. However, the file in the lib
directory is actually called org.eclipselabs.emfjson-0.7.0-SNAPSHOT.jar
. To resolve this, add the <useUniqueVersions>false</useUniqueVersions>
to the configuration of the maven-jar-plugin
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>[...]</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
An example:
-
Installing the
plain-java-project.jar
:mvn install:install-file -Dfile=plain-java-project.jar -DgroupId=plain-java-project -DartifactId=plain-java-project -Dversion=1.0-SNAPSHOT -Dpackaging=jar
-
Add the following dependency:
<dependency> <groupId>plain-java-project</groupId> <artifactId>plain-java-project</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
Bash snippets to modular Maven project:
rm -rf trainbenchmark
mvn archetype:generate \
-B \
-DarchetypeGroupId=org.codehaus.mojo.archetypes \
-DarchetypeArtifactId=pom-root \
-DarchetypeVersion=1.1 \
-DgroupId=hu.bme.mit.trainbenchmark \
-DartifactId=trainbenchmark \
-Dpackage=hu.bme.mit.trainbenchmark \
-Dversion=1.0-SNAPSHOT
cd trainbenchmark/
mvn archetype:create -DgroupId=hu.bme.mit.trainbenchmark -DartifactId=train1
mvn archetype:create -DgroupId=hu.bme.mit.trainbenchmark -DartifactId=train2
mvn clean install
- http://rostislav-matl.blogspot.hu/2011/12/maven-aggregator-vs-parent.html
- http://stackoverflow.com/questions/1992213/maven-parent-pom-vs-modules-pom
Maven only runs the tests from classes whose name ends with Test
.
http://stackoverflow.com/a/174670/90874
[ERROR] /home/szarnyasg/git/incqueryd/hu.bme.mit.incqueryd.runtime/hu.bme.mit.incqueryd.core/src/main/java
[ERROR] /home/szarnyasg/git/incqueryd/hu.bme.mit.incqueryd.runtime/hu.bme.mit.incqueryd.core/src/main/scala
[ERROR] /home/szarnyasg/git/incqueryd/hu.bme.mit.incqueryd.runtime/hu.bme.mit.incqueryd.core/src/test/scala
[INFO] Compiling 50 source files to /home/szarnyasg/git/incqueryd/hu.bme.mit.incqueryd.runtime/hu.bme.mit.incqueryd.core/target/classes
[WARNING] warning: Class inventory.Inventory not found - continuing with a stub.
[WARNING] error: error while loading ArchUtil, class file '/home/szarnyasg/.m2/repository/hu/bme/mit/incqueryd/hu.bme.mit.incqueryd.arch.util/1.0.0-SNAPSHOT/hu.bme.mit.incqueryd.arch.util-1.0.0-SNAPSHOT.jar(hu/bme/mit/incqueryd/arch/util/ArchUtil.class)' is broken
[WARNING] (class java.lang.NullPointerException/null)
[WARNING] /home/szarnyasg/git/incqueryd/hu.bme.mit.incqueryd.runtime/hu.bme.mit.incqueryd.core/src/main/scala/hu/bme/mit/incqueryd/core/rete/actors/CoordinatorActor.scala:35: error: object ScalaChangeSet is not a member of package hu.bme.mit.incqueryd.core.rete.dataunits
[WARNING] import hu.bme.mit.incqueryd.core.rete.dataunits.ScalaChangeSet
[WARNING] ^
[WARNING] one warning found
[WARNING] two errors found
It's not broken, its missing. Make sure that you add the transitive dependencies manually if you mix manifest-first and POM-first projects.
See also: http://gracelessfailures.blogspot.hu/2008/07/class-file-is-broken.html
http://stackoverflow.com/a/10483432/90874
Please check the Troubleshooting section of the Maven and Eclipse page.
Create a settings.xml
file in the ~/.m2
directory.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id></id>
<username></username>
<password></password>
</server>
</servers>
</settings>
Add the following under your POM's <project>
element. Replace /tmp/proj
to a directory of your liking:
<distributionManagement>
<repository>
<id>maven-repository</id>
<url>file:///tmp/proj</url>
</repository>
</distributionManagement>
Use mvn deploy
to deploy.
On more sophisticated methods and the tradeoffs of using GitHub as a Maven repository, read more on Stack Overflow.
-
The Central Repository: the Maven Central (deprecated URL, redirects to Central Sonatype)
- For publication to this repository, see this wiki page.
- Maven Repository: searches in ~200 repositories, including the Maven Central.
- JCenter: fast repository with lots of packages (including a full mirror of the Maven Central), but only provides a very basic search engine. Even if you use JCenter (which is the default in most Gradle projects), you are generally better off searching for snippets in the Central Repository.
- Eclipse Repository: the repository of the Eclipse project, with EMF, OCL, VIATRA, etc.