Skip to content

Commit

Permalink
Gradle Project shall use the Java from the tooling not runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkishalmi committed Feb 4, 2024
1 parent 681aa76 commit 4ebe5a2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@
import org.gradle.api.tasks.testing.Test;
import org.gradle.internal.resolve.ArtifactResolveException;
import org.gradle.jvm.JvmLibrary;
import org.gradle.jvm.toolchain.JavaCompiler;
import org.gradle.language.base.artifact.SourcesArtifact;
import org.gradle.language.java.artifact.JavadocArtifact;
import org.gradle.plugin.use.PluginId;
import org.gradle.api.provider.Property;
import org.gradle.jvm.toolchain.JavaInstallationMetadata;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.util.GradleVersion;
import org.netbeans.modules.gradle.tooling.internal.NbProjectInfo;
import org.netbeans.modules.gradle.tooling.internal.NbProjectInfo.Report;
Expand Down Expand Up @@ -1201,6 +1205,13 @@ private void detectSources(NbProjectInfoModel model) {
o.toString()
);
}

sinceGradle("6.7", () -> {
fetchJavaInstallationMetadata(compileTask).ifPresent(
(meta) -> model.getInfo().put(propBase + lang + "_java_home", meta.getInstallationPath().getAsFile())
);
});

List<String> compilerArgs;

compilerArgs = (List<String>) getProperty(compileTask, "options", "allCompilerArgs");
Expand Down Expand Up @@ -1270,7 +1281,7 @@ private void detectSources(NbProjectInfoModel model) {
model.getInfo().put(propBase + "output_resources", sourceSet.getOutput().getResourcesDir());
sinceGradle("5.2", () -> {
model.getInfo().put(propBase + "GENERATED", storeSet(getProperty(sourceSet, "output", "generatedSourcesDirs", "files")));
});
});
try {
model.getInfo().put(propBase + "classpath_compile", storeSet(sourceSet.getCompileClasspath().getFiles()));
model.getInfo().put(propBase + "classpath_runtime", storeSet(sourceSet.getRuntimeClasspath().getFiles()));
Expand Down Expand Up @@ -1307,6 +1318,18 @@ private void detectSources(NbProjectInfoModel model) {
}
}

private Optional<JavaInstallationMetadata> fetchJavaInstallationMetadata(Task task) {
Property<JavaLauncher> launcherProperty = (Property<JavaLauncher>) getProperty(task, "javaLauncher");
if (launcherProperty != null && launcherProperty.isPresent()) {
return Optional.of(launcherProperty.get().getMetadata());
}
Property<JavaCompiler> compilerProperty = (Property<JavaCompiler>) getProperty(task, "javaCompiler");
if (compilerProperty != null && compilerProperty.isPresent()) {
return Optional.of(compilerProperty.get().getMetadata());
}
return Optional.empty();
}

private void detectArtifacts(NbProjectInfoModel model) {
if (project.getPlugins().hasPlugin("java")) {
model.getInfo().put("main_jar", getProperty(project, "jar", "archivePath"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public final class ProjectInfoDiskCache extends AbstractDiskCache<GradleFiles, QualifiedProjectInfo> {

// Increase this number if new info is gathered from the projects.
private static final int COMPATIBLE_CACHE_VERSION = 24;
private static final int COMPATIBLE_CACHE_VERSION = 25;
private static final String INFO_CACHE_FILE_NAME = "project-info.ser"; //NOI18N
private static final Map<GradleFiles, ProjectInfoDiskCache> DISK_CACHES = Collections.synchronizedMap(new WeakHashMap<>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.openide.filesystems.FileUtil;
import org.openide.util.lookup.ServiceProvider;
import static org.netbeans.modules.gradle.java.api.GradleJavaSourceSet.SourceType;
Expand All @@ -47,7 +48,7 @@ final class GradleJavaProjectBuilder implements ProjectInfoExtractor.Result {
final GradleJavaProject prj = new GradleJavaProject();

GradleJavaProjectBuilder(Map<String, Object> info) {
this.info = info;
this.info = new TreeMap<>(info);
}

GradleJavaProjectBuilder build() {
Expand Down Expand Up @@ -85,6 +86,7 @@ void processSourceSets() {

Map<SourceType, String> sourceComp = new EnumMap<>(SourceType.class);
Map<SourceType, String> targetComp = new EnumMap<>(SourceType.class);
Map<SourceType, File> javaHomes = new EnumMap<>(SourceType.class);
Map<SourceType, List<String>> compilerArgs = new EnumMap<>(SourceType.class);
for (SourceType lang : Arrays.asList(JAVA, GROOVY, SCALA, KOTLIN)) {
String sc = (String) info.get("sourceset_" + name + "_" + lang.name() + "_source_compatibility");
Expand All @@ -95,6 +97,10 @@ void processSourceSets() {
if (tc != null) {
targetComp.put(lang, tc);
}
File javaHome = (File) info.get("sourceset_" + name + "_" + lang.name() + "_java_home");
if (javaHome != null) {
javaHomes.put(lang, javaHome);
}
List<String> compArgs = (List<String>) info.get("sourceset_" + name + "_" + lang.name() + "_compiler_args");
if (compArgs != null) {
compilerArgs.put(lang, Collections.unmodifiableList(compArgs));
Expand All @@ -107,6 +113,7 @@ void processSourceSets() {
}
sourceSet.sourcesCompatibility = Collections.unmodifiableMap(sourceComp);
sourceSet.targetCompatibility = Collections.unmodifiableMap(targetComp);
sourceSet.javaHomes = Collections.unmodifiableMap(javaHomes);
sourceSet.compilerArgs = Collections.unmodifiableMap(compilerArgs);

for (File out : sourceSet.getOutputClassDirs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static enum ClassPathType {

Map<SourceType, String> sourcesCompatibility = Collections.emptyMap();
Map<SourceType, String> targetCompatibility = Collections.emptyMap();
Map<SourceType, File> javaHomes = Collections.emptyMap();
Map<SourceType, List<String>> compilerArgs = Collections.emptyMap();
boolean testSourceSet;
Set<File> outputClassDirs;
Expand Down Expand Up @@ -600,6 +601,10 @@ public String getBuildTaskName(SourceType type) {
return null;
}

public File getJavaHome(SourceType type) {
return javaHomes.get(type);
}

/**
* Returns the compiler arguments for this source set defined for the given
* language.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.netbeans.modules.gradle.api.execute.RunUtils;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -32,26 +33,30 @@
import org.netbeans.api.java.platform.JavaPlatform;
import org.netbeans.api.java.platform.JavaPlatformManager;
import org.netbeans.api.project.Project;
import org.netbeans.modules.gradle.java.api.GradleJavaSourceSet;
import static org.netbeans.modules.gradle.java.api.GradleJavaSourceSet.SourceType.JAVA;
import org.netbeans.modules.gradle.java.execute.JavaRunUtils;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.WeakListeners;

/**
*
* @author Laszlo Kishalmi
*/
public final class BootClassPathImpl extends AbstractGradleClassPathImpl implements PropertyChangeListener {
public final class BootClassPathImpl extends AbstractSourceSetClassPathImpl implements PropertyChangeListener {
private static final String PROTOCOL_NBJRT = "nbjrt"; //NOI18N

JavaPlatformManager platformManager;
final boolean modulesOnly;

public BootClassPathImpl(Project proj) {
this(proj, false);
public BootClassPathImpl(Project proj, String group) {
this(proj, group, false);
}

@SuppressWarnings("LeakingThisInConstructor")
public BootClassPathImpl(Project proj, boolean modulesOnly) {
super(proj);
public BootClassPathImpl(Project proj, String group, boolean modulesOnly) {
super(proj, group);
this.modulesOnly = modulesOnly;
platformManager = JavaPlatformManager.getDefault();
platformManager.addPropertyChangeListener(WeakListeners.propertyChange(this, platformManager));
Expand All @@ -69,7 +74,9 @@ public void propertyChange(PropertyChangeEvent evt) {

@Override
protected List<URL> createPath() {
JavaPlatform platform = JavaRunUtils.getActivePlatform(project).second();
GradleJavaSourceSet ss = getSourceSet();
File jh = ss != null ? ss.getJavaHome(JAVA) : null;
JavaPlatform platform = jh != null ? platformByHome(jh) : JavaRunUtils.getActivePlatform(project).second();
List<URL> ret = new LinkedList<>();
if (platform != null) {
for (ClassPath.Entry entry : platform.getBootstrapLibraries().entries()) {
Expand All @@ -81,6 +88,19 @@ protected List<URL> createPath() {
}
return ret;
}

private JavaPlatform platformByHome(File home) {
FileObject h = FileUtil.toFileObject(home);
for (JavaPlatform platform : platformManager.getInstalledPlatforms()) {
if (platform.isValid()) {
FileObject ph = platform.getInstallFolders().iterator().next();
if (ph.equals(h)) {
return platform;
}
}
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private synchronized ClassPath getModuleLegacyRuntimeClassPath() {

private synchronized ClassPath getBootClassPath() {
if (boot == null) {
boot = ClassPathFactory.createClassPath(new BootClassPathImpl(project, false));
boot = ClassPathFactory.createClassPath(new BootClassPathImpl(project, group, false));
}
return boot;
}
Expand Down Expand Up @@ -339,7 +339,7 @@ private synchronized ClassPath getJava8AnnotationProcessorPath() {

private synchronized ClassPath getPlatformModulesPath() {
if (platformModules == null) {
platformModules = ClassPathFactory.createClassPath(new BootClassPathImpl(project, true));
platformModules = ClassPathFactory.createClassPath(new BootClassPathImpl(project, group, true));
}
return platformModules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private ClassPath getClassPath(String type, boolean excludeTests) {
if (cp == null) {
switch (type) {
case ClassPath.BOOT:
cp = createClassPath(new BootClassPathImpl(project));
cp = createClassPath(new BootClassPathImpl(project, null));
break;
case ClassPath.SOURCE:
cp = createClassPath(new GradleGlobalClassPathImpl.ProjectSourceClassPathImpl(project, excludeTests));
Expand Down

0 comments on commit 4ebe5a2

Please sign in to comment.