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

Adds a testing block to jvm software types #104

Merged
merged 2 commits into from
Jul 2, 2024
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
@@ -1,16 +1,9 @@
package org.gradle.api.experimental.common;

import org.gradle.api.artifacts.dsl.Dependencies;
import org.gradle.api.artifacts.dsl.DependencyCollector;
import org.gradle.declarative.dsl.model.annotations.Restricted;

/**
* The declarative dependencies DSL block for an application.
*/
@SuppressWarnings("UnstableApiUsage")
@Restricted
public interface ApplicationDependencies extends Dependencies {
DependencyCollector getImplementation();
DependencyCollector getRuntimeOnly();
DependencyCollector getCompileOnly();
}
public interface ApplicationDependencies extends BasicDependencies {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gradle.api.experimental.common;


import org.gradle.api.artifacts.dsl.Dependencies;
import org.gradle.api.artifacts.dsl.DependencyCollector;

/**
* Basic types of dependencies used by either an application or library, for production or test code.
*/
@SuppressWarnings("UnstableApiUsage")
public interface BasicDependencies extends Dependencies {
DependencyCollector getImplementation();
DependencyCollector getRuntimeOnly();
DependencyCollector getCompileOnly();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.gradle.api.experimental.common;

import org.gradle.api.artifacts.dsl.Dependencies;
import org.gradle.api.artifacts.dsl.DependencyCollector;
import org.gradle.declarative.dsl.model.annotations.Restricted;

Expand All @@ -9,11 +8,8 @@
*/
@SuppressWarnings("UnstableApiUsage")
@Restricted
public interface LibraryDependencies extends Dependencies {
public interface LibraryDependencies extends BasicDependencies {
DependencyCollector getApi();
DependencyCollector getImplementation();
DependencyCollector getRuntimeOnly();
DependencyCollector getCompileOnly();

// CompileOnlyApi is not included here, since both Android and KMP do not support it.
// Does that mean we should also reconsider if we should support it? Or, should we
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package org.gradle.api.experimental.java;

import org.gradle.api.Action;
import org.gradle.api.experimental.common.HasCliExecutables;
import org.gradle.api.experimental.jvm.HasJavaTarget;
import org.gradle.api.experimental.jvm.HasJvmApplication;
import org.gradle.api.experimental.jvm.extensions.testing.Testing;
import org.gradle.api.tasks.Nested;
import org.gradle.declarative.dsl.model.annotations.Configuring;
import org.gradle.declarative.dsl.model.annotations.Restricted;

/**
* An application implemented using a single version of Java.
*/
@Restricted
public interface JavaApplication extends HasJavaTarget, HasJvmApplication, HasCliExecutables {
@Nested
Testing getTesting();

@Configuring
default void testing(Action<? super Testing> action) {
action.execute(getTesting());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package org.gradle.api.experimental.java;

import org.gradle.api.Action;
import org.gradle.api.experimental.jvm.HasJavaTarget;
import org.gradle.api.experimental.common.HasLibraryDependencies;
import org.gradle.api.experimental.jvm.extensions.testing.Testing;
import org.gradle.api.tasks.Nested;
import org.gradle.declarative.dsl.model.annotations.Configuring;
import org.gradle.declarative.dsl.model.annotations.Restricted;

/**
* A library implemented using a single version of Java.
*/
@Restricted
public interface JavaLibrary extends HasJavaTarget, HasLibraryDependencies {
@Nested
Testing getTesting();

@Configuring
default void testing(Action<? super Testing> action) {
action.execute(getTesting());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private void linkDslModelToPlugin(Project project, JavaApplication dslModel) {
JvmPluginSupport.linkJavaVersion(project, dslModel);
JvmPluginSupport.linkApplicationMainClass(project, dslModel);
JvmPluginSupport.linkMainSourceSourceSetDependencies(project, dslModel.getDependencies());
JvmPluginSupport.linkTestSourceSourceSetDependencies(project, dslModel.getTesting().getDependencies());

dslModel.getRunTasks().add(project.getTasks().named("run"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public void apply(Project project) {
private void linkDslModelToPlugin(Project project, JavaLibrary dslModel) {
JvmPluginSupport.linkJavaVersion(project, dslModel);
JvmPluginSupport.linkMainSourceSourceSetDependencies(project, dslModel.getDependencies());
JvmPluginSupport.linkTestSourceSourceSetDependencies(project, dslModel.getTesting().getDependencies());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.gradle.api.experimental.jvm;

import org.gradle.api.Action;
import org.gradle.api.experimental.common.ApplicationDependencies;
import org.gradle.api.experimental.common.HasApplicationDependencies;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Nested;
import org.gradle.declarative.dsl.model.annotations.Configuring;
import org.gradle.declarative.dsl.model.annotations.Restricted;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.gradle.api.Action;
import org.gradle.api.Named;
import org.gradle.api.experimental.common.LibraryDependencies;
import org.gradle.api.experimental.jvm.extensions.testing.Testing;
import org.gradle.api.tasks.Nested;
import org.gradle.declarative.dsl.model.annotations.Configuring;
import org.gradle.declarative.dsl.model.annotations.Restricted;
Expand All @@ -18,4 +19,11 @@ default void dependencies(Action<? super LibraryDependencies> action) {
action.execute(getDependencies());
}

@Nested
Testing getTesting();

@Configuring
default void testing(Action<? super Testing> action) {
action.execute(getTesting());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract public class StandaloneJvmApplicationPlugin implements Plugin<Project>

public static final String JVM_APPLICATION = "jvmApplication";

@SuppressWarnings("UnstableApiUsage")
@SoftwareType(name = JVM_APPLICATION, modelPublicType = JvmApplication.class)
abstract public JvmApplication getJvmApplication();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abstract public class StandaloneJvmLibraryPlugin implements Plugin<Project> {

public static final String JVM_LIBRARY = "jvmLibrary";

@SuppressWarnings("UnstableApiUsage")
@SoftwareType(name = JVM_LIBRARY, modelPublicType = JvmLibrary.class)
abstract public JvmLibrary getJvmLibrary();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.gradle.api.experimental.jvm.extensions.testing;

import org.gradle.api.experimental.common.BasicDependencies;
import org.gradle.declarative.dsl.model.annotations.Restricted;

@Restricted
public interface TestDependencies extends BasicDependencies {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.gradle.api.experimental.jvm.extensions.testing;

import org.gradle.api.Action;
import org.gradle.api.tasks.Nested;
import org.gradle.declarative.dsl.model.annotations.Configuring;
import org.gradle.declarative.dsl.model.annotations.Restricted;

@Restricted
public interface Testing {
@Nested
TestDependencies getDependencies();

@Configuring
default void dependencies(Action<? super TestDependencies> action) {
action.execute(getDependencies());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.gradle.api.Project;
import org.gradle.api.experimental.common.ApplicationDependencies;
import org.gradle.api.experimental.common.BasicDependencies;
import org.gradle.api.experimental.common.LibraryDependencies;
import org.gradle.api.experimental.jvm.HasJavaTarget;
import org.gradle.api.experimental.jvm.HasJavaTargets;
import org.gradle.api.experimental.jvm.HasJvmApplication;
import org.gradle.api.experimental.jvm.JavaTarget;
import org.gradle.api.experimental.jvm.extensions.testing.TestDependencies;
import org.gradle.api.file.Directory;
import org.gradle.api.plugins.JavaApplication;
import org.gradle.api.plugins.JavaPluginExtension;
Expand All @@ -19,26 +21,6 @@
import java.util.Collections;

public class JvmPluginSupport {
public static void linkSourceSetToDependencies(Project project, SourceSet sourceSet, LibraryDependencies dependencies) {
project.getConfigurations().getByName(sourceSet.getImplementationConfigurationName())
.getDependencies().addAllLater(dependencies.getImplementation().getDependencies());
project.getConfigurations().getByName(sourceSet.getCompileOnlyConfigurationName())
.getDependencies().addAllLater(dependencies.getCompileOnly().getDependencies());
project.getConfigurations().getByName(sourceSet.getRuntimeOnlyConfigurationName())
.getDependencies().addAllLater(dependencies.getRuntimeOnly().getDependencies());
project.getConfigurations().getByName(sourceSet.getApiConfigurationName())
.getDependencies().addAllLater(dependencies.getApi().getDependencies());
}

public static void linkSourceSetToDependencies(Project project, SourceSet sourceSet, ApplicationDependencies dependencies) {
project.getConfigurations().getByName(sourceSet.getImplementationConfigurationName())
.getDependencies().addAllLater(dependencies.getImplementation().getDependencies());
project.getConfigurations().getByName(sourceSet.getCompileOnlyConfigurationName())
.getDependencies().addAllLater(dependencies.getCompileOnly().getDependencies());
project.getConfigurations().getByName(sourceSet.getRuntimeOnlyConfigurationName())
.getDependencies().addAllLater(dependencies.getRuntimeOnly().getDependencies());
}

public static void linkMainSourceSourceSetDependencies(Project project, LibraryDependencies dependencies) {
JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
linkSourceSetToDependencies(project, java.getSourceSets().getByName("main"), dependencies);
Expand All @@ -49,11 +31,34 @@ public static void linkMainSourceSourceSetDependencies(Project project, Applicat
linkSourceSetToDependencies(project, java.getSourceSets().getByName("main"), dependencies);
}

public static void linkTestSourceSourceSetDependencies(Project project, TestDependencies dependencies) {
JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
linkSourceSetToDependencies(project, java.getSourceSets().getByName("main"), dependencies);
}

@SuppressWarnings("UnstableApiUsage")
public static void linkSourceSetToDependencies(Project project, SourceSet sourceSet, BasicDependencies dependencies) {
project.getConfigurations().getByName(sourceSet.getImplementationConfigurationName())
.getDependencies().addAllLater(dependencies.getImplementation().getDependencies());
project.getConfigurations().getByName(sourceSet.getCompileOnlyConfigurationName())
.getDependencies().addAllLater(dependencies.getCompileOnly().getDependencies());
project.getConfigurations().getByName(sourceSet.getRuntimeOnlyConfigurationName())
.getDependencies().addAllLater(dependencies.getRuntimeOnly().getDependencies());
}

@SuppressWarnings("UnstableApiUsage")
public static void linkSourceSetToDependencies(Project project, SourceSet sourceSet, LibraryDependencies dependencies) {
linkSourceSetToDependencies(project, sourceSet, (BasicDependencies) dependencies);
project.getConfigurations().getByName(sourceSet.getApiConfigurationName())
.getDependencies().addAllLater(dependencies.getApi().getDependencies());
}

public static void linkJavaVersion(Project project, HasJavaTarget dslModel) {
JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
java.getToolchain().getLanguageVersion().set(dslModel.getJavaVersion().map(JavaLanguageVersion::of));
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
public static void linkJavaVersion(Project project, HasJavaTargets dslModel) {
JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
java.getToolchain().getLanguageVersion().set(project.provider(() ->
Expand Down
Loading