Skip to content

Commit

Permalink
Add DSL to define versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Jun 26, 2023
1 parent 3b4d07e commit 9274ae0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pluginPublishConventions {
}
}

gradlePlugin.plugins.create("java-module-versions") {
id = "${project.group}.${name}"
implementationClass = "org.gradlex.javamodule.dependencies.JavaModuleVersionsPlugin"
}

tasks.test {
useJUnitPlatform()
maxParallelForks = 4
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright the GradleX team.
*
* 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
*
* http://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 org.gradlex.javamodule.dependencies;

import org.gradle.api.NonNullApi;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.plugins.JavaPlatformPlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradlex.javamodule.dependencies.dsl.ModuleVersions;

@SuppressWarnings("unused")
@NonNullApi
public abstract class JavaModuleVersionsPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
project.getPlugins().withType(JavaPlugin.class, javaPlugin -> setupForProject(project));
project.getPlugins().withType(JavaPlatformPlugin.class, javaPlugin -> setupForProject(project));
}

private void setupForProject(Project project) {
project.getPlugins().apply(JavaModuleDependenciesPlugin.class);
JavaModuleDependenciesExtension javaModuleDependencies = project.getExtensions().getByType(JavaModuleDependenciesExtension.class);
Configuration configuration = project.getConfigurations().findByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME);
if (configuration == null) {
configuration = project.getConfigurations().getByName(JavaPlatformPlugin.API_CONFIGURATION_NAME);
}
project.getExtensions().create("moduleInfo", ModuleVersions.class, configuration, javaModuleDependencies);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright the GradleX team.
*
* 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
*
* http://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 org.gradlex.javamodule.dependencies.dsl;

import org.gradle.api.Action;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencyConstraint;
import org.gradle.api.artifacts.MutableVersionConstraint;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradlex.javamodule.dependencies.JavaModuleDependenciesExtension;

import javax.inject.Inject;

abstract public class ModuleVersions {

private final Configuration configuration;
protected final JavaModuleDependenciesExtension javaModuleDependencies;

@Inject
protected abstract DependencyHandler getDependencies();

public ModuleVersions(Configuration configuration, JavaModuleDependenciesExtension javaModuleDependencies) {
this.configuration = configuration;
this.javaModuleDependencies = javaModuleDependencies;
}

public void version(String moduleName, String version) {
version(moduleName, a -> a.require(version));
}

public void version(String moduleName, Action<? super MutableVersionConstraint> version) {
getDependencies().getConstraints().add(configuration.getName(), javaModuleDependencies.ga(moduleName).map(ga -> {
DependencyConstraint dependencyConstraint = getDependencies().getConstraints().create(ga);
dependencyConstraint.version(version);
return dependencyConstraint;
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.gradle.api.tasks.TaskAction;

import javax.inject.Inject;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
Expand Down

0 comments on commit 9274ae0

Please sign in to comment.