Skip to content

Commit

Permalink
Merge pull request #308 from mr-serjey/master
Browse files Browse the repository at this point in the history
Make Gretty aware of Gradle Java Toolchain (v4.x)
  • Loading branch information
boris-petrov authored Jul 10, 2024
2 parents 04f2a3f + dc69fd2 commit 51b56f0
Show file tree
Hide file tree
Showing 30 changed files with 674 additions and 8 deletions.
20 changes: 17 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
path: ${{ env.PRIVATE_REPO }}

test:
name: Gradle ${{ matrix.gradle }} on Java ${{ matrix.java }}
name: Gradle ${{ matrix.gradle }} on Java ${{ matrix.java }}${{ matrix.toolchainJavaVersion && format(' (Java {0} Container)', matrix.toolchainJavaVersion) || '' }}
runs-on: ubuntu-latest
needs: build

Expand All @@ -48,16 +48,30 @@ jobs:
- java: 11
gradle: '7.6.4'

- java: 11
gradle: '7.6.4'
toolchainJavaVersion: 17

- java: 17
gradle: '7.6.4'

- java: 17
gradle: '7.6.4'
toolchainJavaVersion: 21

- java: 17
gradle: '8.6'

- java: 17
gradle: '8.6'
toolchainJavaVersion: 21

env:
TEST_ALL_CONTAINERS: ${{ matrix.container }}
GRADLE_VERSION: ${{ matrix.gradle }}
EXTRA_PROPERTIES: ${{ matrix.properties }}
TOOLCHAIN_JAVA_ARGS: "${{ matrix.toolchainJavaVersion && format('-PtoolchainJavaVersion={0}', matrix.toolchainJavaVersion) || '' }}"
GRADLE_TEST_TASK: "${{ matrix.toolchainJavaVersion && 'testAllJavaToolchain' || 'testAll' }}"

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -92,8 +106,8 @@ jobs:
../gradlew --no-daemon \
--warning-mode all \
-PprivateRepoDir=$PRIVATE_REPO \
$EXTRA_PROPERTIES \
$EXTRA_PROPERTIES $TOOLCHAIN_JAVA_ARGS \
-PgeckoDriverPlatform=linux64 \
-PtestAllContainers=$TEST_ALL_CONTAINERS \
testAll
$GRADLE_TEST_TASK
working-directory: integrationTests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ build
*.iws
bin/
.DS_Store
.docker-gradle/
45 changes: 45 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
FROM ubuntu as base
RUN apt-get update

FROM base as base-utils
ENV _BASH_UTILS_DIR=/root/.bashrc.d
COPY <<-EOF $_BASH_UTILS_DIR/0_on_bash_ready.bash
shopt -s expand_aliases
export _on_bash_ready_i=\$(find $_BASH_UTILS_DIR -type f | wc -l)
function on_bash_ready (){
((_on_bash_ready_i++))
local file="$_BASH_UTILS_DIR/\${_on_bash_ready_i}.bash"
echo "\$@" >> \$file && \
sed -i 's/\r\$//' \$file && \
source \$file
}
EOF
RUN sed -i 's/\r$//' $_BASH_UTILS_DIR/0_on_bash_ready.bash
RUN echo "while read -r FILE; do source \$FILE; done < <( find $_BASH_UTILS_DIR -name '*.bash' | sort)" >> ~/.profile
SHELL ["/bin/bash", "-l", "-c"]


FROM base-utils as firefox
RUN apt-get install -y wget
RUN install -d -m 0755 /etc/apt/keyrings
RUN wget -q https://packages.mozilla.org/apt/repo-signing-key.gpg -O- | tee /etc/apt/keyrings/packages.mozilla.org.asc > /dev/null
RUN echo 'deb [signed-by=/etc/apt/keyrings/packages.mozilla.org.asc] https://packages.mozilla.org/apt mozilla main' | tee -a /etc/apt/sources.list.d/mozilla.list > /dev/null
RUN apt-get update && apt-get install -y firefox-devedition-l10n-eu
RUN ln -s /usr/bin/firefox-devedition /usr/bin/firefox


FROM firefox as firefox-sdkman
RUN apt-get install -y curl unzip zip findutils
RUN curl -s "https://get.sdkman.io?rcupdate=false" | bash
RUN on_bash_ready source /root/.sdkman/bin/sdkman-init.sh

FROM firefox-sdkman as firefox-jdk
ARG JAVA_VERSIONS="8.0.412-amzn"
ENV JAVA_VERSIONS="$JAVA_VERSIONS"
RUN on_bash_ready 'alias install_jdk="sdk install java $1"'
RUN for version in ${JAVA_VERSIONS//,/ } ; do install_jdk $version ; done


FROM firefox-jdk as firefox-jdk-gradle
ARG GRADLE_VERSION="6.9.4"
RUN sdk install gradle $GRADLE_VERSION
61 changes: 61 additions & 0 deletions docker_gradlew.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash
set -e

function build_docker_gradlew_image(){
docker build -t "docker_gradlew" . \
--build-arg JAVA_VERSIONS="$_javas" \
--build-arg GRADLE_VERSION="$_gradle"
}

function run_docker(){
build_docker_gradlew_image "$*"

local working_dir="-w //project/${_working_dir}"

local project_volume="-v //$(realpath .)://project"

local gradle_home_volume=""
if [ "$_gradle_home" ]; then
gradle_home_volume="-v //$(realpath $_gradle_home)://root/.gradle"
fi

local params="$DOCKER_ARGS $project_volume $working_dir $gradle_home_volume"


echo "RUNNING:" docker run --rm -it $params docker_gradlew "$@"
docker run --rm -it $params docker_gradlew "$@"
}

function run_docker_gradle() {
run_docker bash -lc "gradle $*"
}


JDK["8"]="8.0.412-amzn"
JDK["11"]="11.0.23-amzn"
JDK["17"]="17.0.11-amzn"
JDK["21"]="21.0.3-amzn"

GRADLE["6"]="6.9.4"
GRADLE["7"]="7.6.4"
GRADLE["8"]="8.6"

POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case "$1" in
-j|--java) export _javas+=",${JDK[$2]:=$2}" && shift 2 ;;
-g|--gradle) export _gradle=${GRADLE[$2]:=$2} && shift 2 ;;
-h|--gradle-home) export _gradle_home=$2 && shift 2 ;;
-w|--working-dir) export _working_dir=$2 && shift 2 ;;
-b|--bash) export _bash="Yes" && shift 1 ;;

*) POSITIONAL_ARGS+=("$1") && shift ;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters

if [ "$_bash" ]; then
run_docker bash -l
else
run_docker_gradle "${@}"
fi
71 changes: 71 additions & 0 deletions docker_integration_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash
set -e

export common_gradle_args="--console=plain --no-daemon -Porg.gradle.java.installations.auto-download=false -PgeckoDriverPlatform=linux64"

#ci.yml plugin build step
./docker_gradlew.sh \
--java 11 \
--gradle 7 \
--gradle-home .docker-gradle \
$common_gradle_args \
publishToPrivateRepo

#ci.yml matrix case #1
./docker_gradlew.sh \
--java 11 \
--gradle 7 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
testAll

#ci.yml matrix case #2
./docker_gradlew.sh \
--java 17 \
--gradle 7 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
testAll

#ci.yml matrix case #3
./docker_gradlew.sh \
--java 17 \
--gradle 8 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
testAll

# a set of tests with java toolchain:

#ci.yml matrix case #1 + toolchain java v17
./docker_gradlew.sh \
--java 17 --java 11 \
--gradle 7 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
-PtoolchainJavaVersion=17 \
testAllJavaToolchain

#ci.yml matrix case #2 + toolchain java v21
./docker_gradlew.sh \
--java 21 --java 17 \
--gradle 7 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
-PtoolchainJavaVersion=21 \
testAllJavaToolchain

#ci.yml matrix case #3 + toolchain java v21
./docker_gradlew.sh \
--java 21 --java 17 \
--gradle 8 \
--gradle-home .docker-gradle \
--working-dir integrationTests \
$common_gradle_args \
-PtoolchainJavaVersion=21 \
testAllJavaToolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.akhikhl.gretty.internal.integrationTests;

import java.util.Objects;

public class AnyJavaVersion implements Comparable<AnyJavaVersion> {
private int majorVersion;

private AnyJavaVersion(int majorVersion) {
this.majorVersion = majorVersion;
}

public int getMajorVersion() {
return majorVersion;
}

public boolean isJava9Compatible() {
return majorVersion >= 9;
}

public boolean isJava10Compatible() {
return majorVersion >= 10;
}

@Override
public int compareTo(AnyJavaVersion o) {
return Integer.compare(this.majorVersion, o.majorVersion);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AnyJavaVersion that = (AnyJavaVersion) o;
return majorVersion == that.majorVersion;
}

@Override
public int hashCode() {
return Objects.hashCode(majorVersion);
}

public static AnyJavaVersion of(Integer integer) {
return new AnyJavaVersion(Objects.requireNonNull(integer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class BasePlugin implements Plugin<Project> {
}

protected void configureExtensions(Project project) {
// does nothing by default
if (!project.extensions.findByName('javaVersion')) {
project.extensions.add(AnyJavaVersion, 'javaVersion', JavaToolchainIntegrationTestPlugin.getToolchainJavaVersion(project))
}
}

protected void configurePublications(Project project) {
Expand Down Expand Up @@ -98,6 +100,9 @@ class BasePlugin implements Plugin<Project> {
if(!project.rootProject.tasks.findByName('testAll'))
project.rootProject.task 'testAll'

if(!project.rootProject.tasks.findByName('testAllJavaToolchain'))
project.rootProject.task 'testAllJavaToolchain'

project.tasks.withType(Test).configureEach {
if (GradleVersion.current().baseVersion.version.startsWith("7.")) {
useJUnitPlatform()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class IntegrationTestPlugin extends BasePlugin {
protected void configureExtensions(Project project) {
super.configureExtensions(project)

/**
* Makes the project aware of java toolchain if it has -PtoolchainJavaVersion=17 parameter.
* Toolchain DSL is configured automatically for the provided version of java.
**/
project.ext.defineAsJavaToolchainAwareIntegrationTest = {
JavaToolchainIntegrationTestPlugin.applyPluginConditionally(project)
}

project.ext.defineIntegrationTest = {

def integrationTestTask_ = project.tasks.findByName('integrationTest')
Expand All @@ -61,6 +69,10 @@ class IntegrationTestPlugin extends BasePlugin {
else
testClassesDirs = project.sourceSets.integrationTest.output.classesDirs
classpath = project.sourceSets.integrationTest.runtimeClasspath

JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceTaskToUseGradleJvm(it)
}
}

integrationTestTask_
Expand All @@ -75,6 +87,10 @@ class IntegrationTestPlugin extends BasePlugin {

integrationTestAllContainersTask = project.task('integrationTestAllContainers')

JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceTaskToUseGradleJvm(integrationTestAllContainersTask)
}

if (!integrationTestContainers)
integrationTestContainers = ServletContainerConfig.getConfigNames().collect() // returns immutable and we want to filter later

Expand All @@ -92,6 +108,10 @@ class IntegrationTestPlugin extends BasePlugin {
else
testClassesDirs = project.sourceSets.integrationTest.output.classesDirs
classpath = project.sourceSets.integrationTest.runtimeClasspath

JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceTaskToUseGradleJvm(thisTask)
}
}

integrationTestAllContainersTask.dependsOn project.tasks['integrationTest_' + container]
Expand Down Expand Up @@ -169,6 +189,10 @@ class IntegrationTestPlugin extends BasePlugin {
srcDir 'src/integrationTest/resources'
}
runtimeClasspath += project.rootProject.files('config/gebConfig')

JavaToolchainIntegrationTestPlugin.whenApplied(project) { plugin ->
plugin.forceSourceSetToUseGradleJvm(project, it)
}
}
}
}
Expand Down
Loading

0 comments on commit 51b56f0

Please sign in to comment.