Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle init shall honor the configured Java Runtime #8223

Merged
merged 1 commit into from
Feb 6, 2025
Merged
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 @@ -59,11 +59,12 @@
import org.netbeans.modules.gradle.ProjectTrust;
import org.netbeans.modules.gradle.api.GradleProjects;
import org.netbeans.modules.gradle.api.NbGradleProject;
import org.netbeans.modules.gradle.api.NbGradleProject.LoadOptions;
import org.netbeans.modules.gradle.api.NbGradleProject.Quality;
import org.netbeans.modules.gradle.execute.EscapeProcessingOutputStream;
import org.netbeans.modules.gradle.execute.GradlePlainEscapeProcessor;
import org.netbeans.modules.gradle.options.GradleExperimentalSettings;
import org.netbeans.modules.gradle.spi.GradleSettings;
import org.netbeans.modules.gradle.spi.execute.JavaRuntimeManager;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.util.Exceptions;
Expand Down Expand Up @@ -312,6 +313,8 @@ public String getMessage() {
@Override
public Set<FileObject> execute() {
GradleConnector gconn = GradleConnector.newConnector();
JavaRuntimeManager.JavaRuntime defaultRuntime = GradleExperimentalSettings.getDefault().getDefaultJavaRuntime();

target.mkdirs();
InputOutput io = IOProvider.getDefault().getIO(projectName + " (init)", true);
try (ProjectConnection pconn = gconn.forProjectDirectory(target).connect()) {
Expand Down Expand Up @@ -359,7 +362,8 @@ public Set<FileObject> execute() {
OutputStream out = new EscapeProcessingOutputStream(new GradlePlainEscapeProcessor(io, false));
OutputStream err = new EscapeProcessingOutputStream(new GradlePlainEscapeProcessor(io, false))
) {
BuildLauncher gradleInit = pconn.newBuild().forTasks(args.toArray(new String[0]));
BuildLauncher gradleInit = pconn.newBuild().forTasks(args.toArray(String[]::new));
gradleInit.setJavaHome(defaultRuntime.getJavaHome());
if (GradleSettings.getDefault().isOffline()) {
gradleInit = gradleInit.withArguments("--offline");
}
Expand All @@ -370,7 +374,7 @@ public Set<FileObject> execute() {
} catch (IOException iox) {
}
} catch (GradleConnectionException | IllegalStateException ex) {
Exceptions.printStackTrace(ex);
ex.printStackTrace(io.getErr());
} finally {
if (io.getOut() != null) io.getOut().close();
if (io.getErr() != null) io.getErr().close();
Expand Down Expand Up @@ -444,6 +448,7 @@ public Set<FileObject> execute() {
FileUtil.createFolder(dir);
Thread.sleep(200);
} catch (InterruptedException | IOException ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
Expand Down Expand Up @@ -479,6 +484,7 @@ public final Set<FileObject> execute() {
}

} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
return Set.of();
Expand Down Expand Up @@ -542,6 +548,7 @@ public Set<FileObject> execute() {
return ret;
}
} catch (IOException | IllegalArgumentException ex) {
Exceptions.printStackTrace(ex);
}
}
return null;
Expand All @@ -568,21 +575,24 @@ public String getMessage() {
@Override
public Set<FileObject> execute() {
GradleConnector gconn = GradleConnector.newConnector();
JavaRuntimeManager.JavaRuntime defaultRuntime = GradleExperimentalSettings.getDefault().getDefaultJavaRuntime();
try (ProjectConnection pconn = gconn.forProjectDirectory(projectDir).connect()) {
List<String> args = new ArrayList<>();
args.add("wrapper"); //NOI18N
if (version != null) {
args.add("--gradle-version"); //NOI18N
args.add(version);
}
BuildLauncher init = pconn.newBuild()
.setJavaHome(defaultRuntime.getJavaHome());
if (GradleSettings.getDefault().isOffline()) {
pconn.newBuild().withArguments("--offline").forTasks(args.toArray(new String[0])).run(); //NOI18N
} else {
pconn.newBuild().forTasks(args.toArray(new String[0])).run();
init = init.withArguments("--offline");
}
init.forTasks(args.toArray(String[]::new)).run();
} catch (GradleConnectionException | IllegalStateException ex) {
// Well for some reason we were not able to load Gradle.
// Ignoring that for now
Exceptions.printStackTrace(ex);
}
gconn.disconnect();
return null;
Expand Down Expand Up @@ -684,7 +694,7 @@ public Set<FileObject> execute() {
DataObject newData = o.createFromTemplate(targetFolder, targetName, tokens);
return important ? Set.of(newData.getPrimaryFile()) : null;
} catch (IOException ex) {

Exceptions.printStackTrace(ex);
}
}
return null;
Expand Down
Loading