Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1205,6 +1209,13 @@ private void detectSources(NbProjectInfoModel model) {
o.toString()
);
}

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

List<String> compilerArgs;

compilerArgs = (List<String>) getProperty(compileTask, "options", "allCompilerArgs");
Expand Down Expand Up @@ -1311,6 +1322,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
22 changes: 22 additions & 0 deletions java/gradle.java/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ is the proper place.
<!-- ACTUAL CHANGES BEGIN HERE: -->

<changes>
<change id="sourceset-compiler-javahome">
<api name="gradle.java.api"/>
<summary>Support for per-language output directories</summary>
<version major="1" minor="26"/>
<date day="4" month="1" year="2024"/>
<author login="lkishalmi"/>
<compatibility semantic="compatible" addition="yes" deprecation="no"/>
<description>
<p>
Gradle 6.7 introduced Java Toolchains to separate Gradle Java Runtime
and the Java used for compilation (and other Java execution).
<code><a href="@TOP@/org/netbeans/modules/gradle/java/api/GradleJavaSourceSet.html#getCompilerJavaHome-org.netbeans.modules.gradle.java.api.GradleJavaSourceSet.SourceType-">GradleJavaSourceSet.getCompilerJavaHome</a></code> has been added to
return the Java Home of the JDK in use for compilation.
</p>
<p>
In addition <code><a href="@TOP@/org/netbeans/modules/gradle/java/spi/support/JavaToolchainSupport.html">JavaToolchainSsupport</a></code>
is provided in order to be easily work with the JDK home directories.
</p>
</description>
<class package="org.netbeans.modules.gradle.java.api" name="GradleJavaSourceSet"/>
<class package="org.netbeans.modules.gradle.java.spi.support" name="JavaToolchainSupport"/>
</change>
<change id="sourceset-lang-output">
<api name="gradle.java.api"/>
<summary>Support for per-language output directories</summary>
Expand Down
1 change: 1 addition & 0 deletions java/gradle.java/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ AutoUpdate-Show-In-Client: false
OpenIDE-Module: org.netbeans.modules.gradle.java
OpenIDE-Module-Layer: org/netbeans/modules/gradle/java/layer.xml
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/gradle/java/Bundle.properties
OpenIDE-Module-Java-Dependencies: Java > 17
OpenIDE-Module-Implementation-Version: 1
3 changes: 2 additions & 1 deletion java/gradle.java/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# under the License.

is.autoload=true
javac.source=1.8
javac.source=17
javac.target=17
javac.compilerargs=-Xlint -Xlint:-serial
nbm.module.author=Laszlo Kishalmi
javadoc.arch=${basedir}/arch.xml
Expand Down
1 change: 1 addition & 0 deletions java/gradle.java/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@
<package>org.netbeans.modules.gradle.java.api</package>
<package>org.netbeans.modules.gradle.java.api.output</package>
<package>org.netbeans.modules.gradle.java.spi.debug</package>
<package>org.netbeans.modules.gradle.java.spi.support</package>
</public-packages>
</data>
</configuration>
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() + "_compiler_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.compilerJavaHomes = 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> compilerJavaHomes = Collections.emptyMap();
Map<SourceType, List<String>> compilerArgs = Collections.emptyMap();
boolean testSourceSet;
Set<File> outputClassDirs;
Expand Down Expand Up @@ -600,6 +601,22 @@ public String getBuildTaskName(SourceType type) {
return null;
}

/**
* Returns the JDK Home directory of the JVM what would be used during the
* compilation. Currently the {@linkplain SourceType#JAVA JAVA}, {@linkplain SourceType#GROOVY GROOVY}, and {@linkplain SourceType#SCALA SCALA}
* are expected to return a non {@code null} value. The home directory
* is determined by using the sourceSet default compile task. In Gradle
* it is possible to define additional compile tasks with different Java Toolchain.
* NetBeans would ignore those.
*
* @param type The source type of the compiler.
* @return The home directory of the JDK used for the default compile task.
* @since 1.26
*/
public File getCompilerJavaHome(SourceType type) {
return compilerJavaHomes.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 @@ -25,12 +25,12 @@
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.project.Project;
Expand Down Expand Up @@ -105,10 +105,7 @@ private boolean hasChanged(List<URL> oldValue, List<URL> newValue) {
@Override
public final synchronized List<? extends PathResourceImplementation> getResources() {
if (resources == null) {
resources = new ArrayList<>();
for (URL url : createPath()) {
resources.add(ClassPathSupport.createResource(url));
}
resources = createPath().stream().map(ClassPathSupport::createResource).toList();
}
return resources;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,44 @@

package org.netbeans.modules.gradle.java.classpath;

import org.netbeans.modules.gradle.api.NbGradleProject;
import org.netbeans.modules.gradle.api.execute.RunUtils;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.netbeans.modules.gradle.java.spi.support.JavaToolchainSupport;
import java.io.File;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import org.netbeans.api.java.classpath.ClassPath;
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.util.WeakListeners;

/**
*
* @author Laszlo Kishalmi
*/
public final class BootClassPathImpl extends AbstractGradleClassPathImpl implements PropertyChangeListener {
public final class BootClassPathImpl extends AbstractSourceSetClassPathImpl {
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));
NbGradleProject.getPreferences(project, false).addPreferenceChangeListener((PreferenceChangeEvent evt) -> {
if (RunUtils.PROP_JDK_PLATFORM.equals(evt.getKey())) {
clearResourceCache();
}
});
}

@Override
public void propertyChange(PropertyChangeEvent evt) {
clearResourceCache();
}

@Override
protected List<URL> createPath() {
JavaPlatform platform = JavaRunUtils.getActivePlatform(project).second();
JavaToolchainSupport toolchain = JavaToolchainSupport.getDefault();
GradleJavaSourceSet ss = getSourceSet();
File jh = ss != null ? ss.getCompilerJavaHome(JAVA) : null;

JavaPlatform platform = jh != null ? toolchain.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 +68,4 @@ protected List<URL> createPath() {
}
return ret;
}


}
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 @@ -66,43 +66,27 @@ private ClassPath getClassPath(String type, boolean excludeTests) {
if (index < 0) return null;
ClassPath cp = cache[index];
if (cp == null) {
switch (type) {
case ClassPath.BOOT:
cp = createClassPath(new BootClassPathImpl(project));
break;
case ClassPath.SOURCE:
cp = createClassPath(new GradleGlobalClassPathImpl.ProjectSourceClassPathImpl(project, excludeTests));
break;
case ClassPath.COMPILE:
cp = createClassPath(new GradleGlobalClassPathImpl.ProjectCompileClassPathImpl(project, excludeTests));
break;
case ClassPath.EXECUTE:
cp = createClassPath(new GradleGlobalClassPathImpl.ProjectRuntimeClassPathImpl(project, excludeTests));
break;
}
cp = switch (type) {
case ClassPath.BOOT -> createClassPath(new BootClassPathImpl(project, null));
case ClassPath.SOURCE -> createClassPath(new GradleGlobalClassPathImpl.ProjectSourceClassPathImpl(project, excludeTests));
case ClassPath.COMPILE -> createClassPath(new GradleGlobalClassPathImpl.ProjectCompileClassPathImpl(project, excludeTests));
case ClassPath.EXECUTE -> createClassPath(new GradleGlobalClassPathImpl.ProjectRuntimeClassPathImpl(project, excludeTests));
default -> null;
};
cache[index] = cp;
}
return cp;
}

private static int type2Index(String type, boolean excludeTests) {
int index;
switch (type) {
case ClassPath.BOOT:
index = BOOT;
break;
case ClassPath.SOURCE:
index = SOURCE;
break;
case ClassPath.COMPILE:
index = COMPILE;
break;
case ClassPath.EXECUTE:
index = RUNTIME;
break;
default:
index = -1;
}
index = switch (type) {
case ClassPath.BOOT -> BOOT;
case ClassPath.SOURCE -> SOURCE;
case ClassPath.COMPILE -> COMPILE;
case ClassPath.EXECUTE -> RUNTIME;
default -> -1;
};

return (index >= 0) && excludeTests ? index + 1 : index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
SourceSetPanel.jLabel1.text=Output Classes:
SourceSetPanel.jLabel2.text=Output Resources:
SourceSetPanel.jLabel3.text=Source/Binary Format:
SourceSetPanel.lbPlatform.text=Java Platform:
Loading