Skip to content

Commit

Permalink
Demo .mavenrc
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolai Parlog committed Mar 22, 2017
1 parent 585045b commit b9b99e4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -4,5 +4,6 @@ Different ways to build projects on Java 9 with Maven.
The goal is to be able to simply run `mvn install` and then launch the resulting JAR with `java -jar target/demo-java-9.jar`.

* [toolchain](toolchain)
* [.mavenrc](mavenrc)

Some approaches are more intrusive, others more brittle - have a look at the individual solutions for a small discussion of its pros and cons.
19 changes: 19 additions & 0 deletions mavenrc/README.md
@@ -0,0 +1,19 @@
# `mavenrc`

Maven can apparently be configured with the [mostly undocumented][jira-mavenrc-doc] files `~/.mavenrc` (for current user) and `/etc/mavenrc` (for all users).
In there, environment variables and command line options for the Java command can be configured.

With this, it is easy to set `JAVA_HOME` just for the Maven command, which will lead to it running with the specified version.
Here's the content of that file:

```
JAVA_HOME="/path/to/your/jdk-9"
```

## Pros and Cons

If you not only want to compile with Java 9 but also use Java 9 features, you still have to specify the target `1.9` in your [pom](pom.xml).
Note that this puts the pom into an awkward state where it is supposed to use JDK 9 but does not reference where it might come from (unlike the [toolchain approach](../toolchain))


[jira-mavenrc-doc]: https://issues.apache.org/jira/browse/MNGSITE-246
41 changes: 41 additions & 0 deletions mavenrc/pom.xml
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>org.codefx.demo.mvn-java-9</groupId>
<artifactId>demo</artifactId>
<version>java-9</version>

<build>
<plugins>
<!-- target Java 9 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
<release>9</release>
</configuration>
</plugin>
<!-- make resulting JAR executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.codefx.demo.mvn_java9.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

</project>
18 changes: 18 additions & 0 deletions mavenrc/src/main/java/org/codefx/demo/mvn_java9/Main.java
@@ -0,0 +1,18 @@
package org.codefx.demo.mvn_java9;

import static java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE;

public class Main {

public static void main(String[] args) {
printCallerClass();
}

private static void printCallerClass() {
Class<?> callerClass = StackWalker
.getInstance(RETAIN_CLASS_REFERENCE)
.getCallerClass();
System.out.println(callerClass);
}

}

0 comments on commit b9b99e4

Please sign in to comment.