Implement JvmOptions class

This commit is contained in:
Erik C. Thauvin 2025-03-21 00:58:41 -07:00
parent 832447771d
commit 3c5b1fde8f
Signed by: erik
GPG key ID: 776702A6A2DA330E
6 changed files with 140 additions and 10 deletions

View file

@ -1,7 +1,7 @@
bld.downloadExtensionJavadoc=false
bld.downloadExtensionSources=true
bld.downloadLocation=
bld.extension-kotlin=com.uwyn.rife2:bld-kotlin:1.0.4
bld.extension-kotlin=com.uwyn.rife2:bld-kotlin:1.0.5-SNAPSHOT
bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
bld.sourceDirectories=
bld.version=2.2.1

View file

@ -3,6 +3,8 @@ package com.example;
import rife.bld.BuildCommand;
import rife.bld.Project;
import rife.bld.extension.CompileKotlinOperation;
import rife.bld.extension.kotlin.CompileOptions;
import rife.bld.extension.kotlin.JvmOptions;
import java.io.File;
import java.util.List;
@ -58,12 +60,14 @@ public class ExampleBuild extends Project {
@BuildCommand(summary = "Compiles the Kotlin project")
@Override
public void compile() throws Exception {
var options = new CompileOptions().verbose(true);
options.jvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED);
// The source code located in src/main/kotlin and src/test/kotlin will be compiled
var op = new CompileKotlinOperation()
new CompileKotlinOperation()
// .kotlinHome("path/to/kotlin")
// .kotlinc("path/to/kotlinc")
.fromProject(this);
op.compileOptions().verbose(true);
op.execute();
.compileOptions(options)
.fromProject(this)
.execute();
}
}

View file

@ -37,7 +37,7 @@ public class CompileOptions {
private final Collection<String> advancedOptions_ = new ArrayList<>();
private final Collection<File> argFile_ = new ArrayList<>();
private final Collection<File> classpath_ = new ArrayList<>();
private final Collection<String> jvmOptions_ = new ArrayList<>();
private final JvmOptions jvmOptions_ = new JvmOptions();
private final Collection<String> optIn_ = new ArrayList<>();
private final Collection<String> options_ = new ArrayList<>();
private final Collection<String> plugin_ = new ArrayList<>();
@ -709,12 +709,12 @@ public class CompileOptions {
*
* @return the JVM options
*/
public Collection<String> jvmOptions() {
public JvmOptions jvmOptions() {
return jvmOptions_;
}
/**
* Pass an option directly to Java Virtual Machine
* Pass an option directly to the Java Virtual Machine
*
* @param jvmOptions the JVM options
* @return this operation instance
@ -725,7 +725,7 @@ public class CompileOptions {
}
/**
* Pass an option directly to JVM
* Pass an option directly to the Java Virtual Machine
*
* @param jvmOptions one or more JVM option
* @return this operation instance

View file

@ -0,0 +1,61 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rife.bld.extension.kotlin;
import rife.tools.StringUtils;
import java.io.Serial;
import java.util.ArrayList;
import java.util.List;
/**
* Java Virtual Machine options.
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0.5
*/
@SuppressWarnings("PMD.LooseCoupling")
public class JvmOptions extends ArrayList<String> {
@Serial
private static final long serialVersionUID = 1L;
/**
* Keyword to enable native access for all code on the class path.
*/
public final static String ALL_UNNAMED = "ALL-UNNAMED";
/**
* Modules that are permitted to perform restricted native operations.
* The module name can also be {@link #ALL_UNNAMED}.
*
* @return this list of options
*/
public JvmOptions enableNativeAccess(String... modules) {
return enableNativeAccess(List.of(modules));
}
/**
* Modules that are permitted to perform restricted native operations.
* The module name can also be {@link #ALL_UNNAMED}.
*
* @return this list of options
*/
public JvmOptions enableNativeAccess(List<String> modules) {
add("--enable-native-access=" + StringUtils.join(modules, ","));
return this;
}
}

View file

@ -23,6 +23,7 @@ import rife.bld.BaseProject;
import rife.bld.blueprints.BaseProjectBlueprint;
import rife.bld.extension.kotlin.CompileOptions;
import rife.bld.extension.kotlin.CompilerPlugin;
import rife.bld.extension.kotlin.JvmOptions;
import rife.tools.FileUtils;
import java.io.File;
@ -177,9 +178,10 @@ class CompileKotlinOperationTest {
op.compileOptions().verbose(true);
op.compileOptions().jdkRelease("17");
op.compileOptions().jvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED);
var args = op.compileOptions().args();
var matches = List.of("-Xjdk-release=17", "-no-stdlib", "-verbose");
var matches = List.of("-Xjdk-release=17", "-J--enable-native-access=ALL-UNNAMED", "-no-stdlib", "-verbose");
assertThat(args).as(args + " == " + matches).isEqualTo(matches);
op.execute();

View file

@ -0,0 +1,63 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rife.bld.extension.kotlin;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
class JvmOptionsTest {
@Test
void testCompileOptions() {
var compileOptions = new CompileOptions().jvmOptions(new JvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED));
assertThat(compileOptions.jvmOptions()).as(JvmOptions.ALL_UNNAMED).containsExactly("--enable-native-access=ALL-UNNAMED");
assertThat(compileOptions.args()).as("args()").containsExactly("-J--enable-native-access=ALL-UNNAMED");
compileOptions = new CompileOptions().jvmOptions(new JvmOptions().enableNativeAccess("m1", "m2"));
assertThat(compileOptions.jvmOptions()).as("m1,m2").containsExactly("--enable-native-access=m1,m2");
assertThat(compileOptions.args()).as("args(m1,m2)").containsExactly("-J--enable-native-access=m1,m2");
}
@Test
void testEnableNativeAccess() {
var options = new JvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED);
assertThat(options).as(JvmOptions.ALL_UNNAMED).containsExactly("--enable-native-access=ALL-UNNAMED");
options = new JvmOptions().enableNativeAccess("m1", "m2");
assertThat(options).as("m1,m2").containsExactly("--enable-native-access=m1,m2");
}
@Test
void testJvmOptions() {
var compileOptions = new CompileOptions().jvmOptions("option1", "option2");
assertThat(compileOptions.jvmOptions()).as("option1,option2").containsExactly("option1", "option2");
assertThat(compileOptions.args()).as("args()").containsExactly("-Joption1", "-Joption2");
compileOptions = new CompileOptions().jvmOptions(List.of("option1", "option2"));
assertThat(compileOptions.jvmOptions()).as("List.of(option1,option2)").containsExactly("option1", "option2");
assertThat(compileOptions.args()).as("args(list)").containsExactly("-Joption1", "-Joption2");
compileOptions = compileOptions.jvmOptions(new JvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED));
assertThat(compileOptions.jvmOptions()).as("List.of(option1,option2,ALL_UNNAMED)")
.containsExactly("option1", "option2", "--enable-native-access=ALL-UNNAMED");
assertThat(compileOptions.args()).as("args(option1,option2,ALL_UNNAMED)")
.containsExactly("-Joption1", "-Joption2", "-J--enable-native-access=ALL-UNNAMED");
}
}