Skip to content

Commit

Permalink
Merge pull request #48 from caarlos0/gradle
Browse files Browse the repository at this point in the history
Added gradle support
  • Loading branch information
caarlos0 committed Mar 18, 2016
2 parents f7f678b + 87e3702 commit 775192f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
> The _"Java Version Manager"_
Automatically change `JAVA_HOME` and `PATH` based on current directory
`.java-version` or `pom.xml` files.
`.java-version`, `pom.xml` and `build.gradle` files.

The philosophy behind this project is to simplify and automate the `JAVA_HOME`
changing, much like `rbenv` and `rvm` do for Ruby.
Expand All @@ -27,10 +27,12 @@ $ echo "source ~/.jvm/jvm.sh" >> ~/.zshrc
Then, just `cd` to a java project folder. `jvm` will look for a `.java-version`
and use whatever version is inside it. If the file don't exist, but a
`pom.xml` do, `jvm` will try to extract the version from the `pom.xml` file
using a regular expression.
using a regular expression. If no `pom.xml` is found, it will look for the
`build.gradle` file and do the same strategy to extract the java version.

`jvm` can also recursively search for `.java-version` and `pom.xml` files, so,
`cd`-ing to project's subfolder should maintain its version set.
`jvm` can also recursively search for `.java-version`, `pom.xml` and
`build.gradle` files, so, `cd`-ing to project's subfolder should maintain
its version set.

You can always change the current folder java version by doing:

Expand All @@ -47,6 +49,13 @@ example, you can run `jvm config` and add a line like this:
6-openjdk=/path/to/openjdk/6
```

or even:

```properties
6=/path/to/openjdk/6
```


And `jvm` will automagically works.

And, yes, this strategy (based on `jvm config`) can make `jvm` work on Windows
Expand Down
28 changes: 23 additions & 5 deletions jvm.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/sh
# RegExp used to find the java version in a pom file.
JVM_REGEX="<(java.version|maven.compiler.source|source)>1\.[4-9]</.*>"
POM_REGEX="<(java.version|maven.compiler.source|source)>1\.[4-9]</.*>"
# RegExp used to find the java version in a build.gradle file.
GRADLE_REGEX="(sourceCompatibility|targetCompatibility) ?= ?1\.[4-9]"

# finds the java home for the given version
__jvm_javahome() {
Expand Down Expand Up @@ -42,20 +44,33 @@ __jvm_set() {
export PATH="${JAVA_HOME}/bin:$PATH"
}

# tried to find the java version using regex.
# tries to find the java version using regex inside a pom.xml file.
__jvm_pomversion() {
# shellcheck disable=SC2039
local tag pom
pom="$1/pom.xml"
local proj tag pom
proj="$1"
pom="$proj/pom.xml"
test ! -s "$pom" && return 1
tag="$(grep -Eo "$JVM_REGEX" "$pom")"
tag="$(grep -Eo "$POM_REGEX" "$pom")"
test -z "$tag" && return 1
echo "$tag" |
cut -f2 -d'>' |
cut -f2 -d'.' |
cut -f1 -d'<'
}

# tries to find the java version using regex inside a build.gradle file.
__jvm_gradleversion() {
# shellcheck disable=SC2039
local proj property build
proj="$1"
build="$proj/build.gradle"
test ! -s "$build" && return 1
property="$(grep -Eo "$GRADLE_REGEX" "$build" | head -n1)"
test -z "$property" && return 1
echo "$property" | tr -d ' ' | cut -f2 -d '=' | cut -f2 -d'.'
}

# tries to get the version from the local .java-version
__jvm_local_version() {
# shellcheck disable=SC2039
Expand Down Expand Up @@ -83,6 +98,9 @@ __jvm_version() {
# try to extract from pom.xml
test -z "$version" && version="$(__jvm_pomversion "$proj")"

# try to extrat from build.gradle
test -z "$version" && version="$(__jvm_gradleversion "$proj")"

# go up looking for pom.xmls and .java-versions
parent="$proj/.."
test -z "$version" &&
Expand Down
1 change: 1 addition & 0 deletions tests/complex-gradle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sourceCompatibility = 1.7
1 change: 1 addition & 0 deletions tests/complex-gradle/sub/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
1 change: 1 addition & 0 deletions tests/gradle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sourceCompatibility = 1.7
39 changes: 39 additions & 0 deletions tests/test.clitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,45 @@ $ jvm version
$
```

# simple build.gradle

Test a simple `build.gradle` with only one source/target Compatibility property
set.

```console
$ cd "$ROOT/$TESTS/gradle"
$ jvm reload
$ jvm version
7
$
```

# complex build.gradle

Test a simple `build.gradle` with both source and target compatibility property
set.

```console
$ cd "$ROOT/$TESTS/complex-gradle"
$ jvm reload
$ jvm version
7
$
```

# parent build.gradle

When the build.gradle in the current folder don't define the java version,
look for files in the parent folder.

```console
$ cd "$ROOT/$TESTS/complex-gradle/sub"
$ jvm reload
$ jvm version
7
$
```

# jvmconfig

Test that `jvm` respects a custom java version in `~/.jvmconfig`, in this case,
Expand Down

0 comments on commit 775192f

Please sign in to comment.