All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.language.base.plugins.LifecycleBasePlugin Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2014 the original author or authors.
 *
 * 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.gradle.language.base.plugins;

import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Incubating;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.internal.TaskInternal;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.tasks.Delete;
import org.gradle.language.base.internal.plugins.CleanRule;

import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;

/**
 * 

A {@link org.gradle.api.Plugin} which defines a basic project lifecycle.

*/ @Incubating public class LifecycleBasePlugin implements Plugin { public static final String CLEAN_TASK_NAME = "clean"; public static final String ASSEMBLE_TASK_NAME = "assemble"; public static final String CHECK_TASK_NAME = "check"; public static final String BUILD_TASK_NAME = "build"; public static final String BUILD_GROUP = "build"; public static final String VERIFICATION_GROUP = "verification"; private static final String CUSTOM_LIFECYCLE_TASK_ERROR_MSG = "Declaring custom '%s' task when using the standard Gradle lifecycle plugins is not allowed."; private final Set placeholders = new HashSet(); @Override public void apply(ProjectInternal project) { addClean(project); addCleanRule(project); addAssemble(project); addCheck(project); addBuild(project); addDeprecationWarningsAboutCustomLifecycleTasks(project); } private void addClean(final ProjectInternal project) { addPlaceholderAction(project, CLEAN_TASK_NAME, Delete.class, new Action() { @Override public void execute(Delete clean) { clean.setDescription("Deletes the build directory."); clean.setGroup(BUILD_GROUP); clean.delete(new Callable() { public File call() throws Exception { return project.getBuildDir(); } }); } }); } private void addCleanRule(Project project) { project.getTasks().addRule(new CleanRule(project.getTasks())); } private void addAssemble(ProjectInternal project) { addPlaceholderAction(project, ASSEMBLE_TASK_NAME, DefaultTask.class, new Action() { @Override public void execute(TaskInternal assembleTask) { assembleTask.setDescription("Assembles the outputs of this project."); assembleTask.setGroup(BUILD_GROUP); } }); } private void addCheck(ProjectInternal project) { addPlaceholderAction(project, CHECK_TASK_NAME, DefaultTask.class, new Action() { @Override public void execute(TaskInternal checkTask) { checkTask.setDescription("Runs all checks."); checkTask.setGroup(VERIFICATION_GROUP); } }); } private void addBuild(final ProjectInternal project) { addPlaceholderAction(project, BUILD_TASK_NAME, DefaultTask.class, new Action() { @Override public void execute(DefaultTask buildTask) { buildTask.setDescription("Assembles and tests this project."); buildTask.setGroup(BUILD_GROUP); buildTask.dependsOn(ASSEMBLE_TASK_NAME); buildTask.dependsOn(CHECK_TASK_NAME); } }); } void addPlaceholderAction(ProjectInternal project, final String placeholderName, Class type, final Action configure) { placeholders.add(placeholderName); project.getTasks().addPlaceholderAction(placeholderName, type, new Action() { @Override public void execute(T t) { t.getExtensions().getExtraProperties().set("placeholder", true); configure.execute(t); } }); } private void addDeprecationWarningsAboutCustomLifecycleTasks(ProjectInternal project) { project.getTasks().all(new Action() { @Override public void execute(Task task) { if (placeholders.contains(task.getName()) && !task.getExtensions().getExtraProperties().has("placeholder")) { throw new InvalidUserDataException(String.format(CUSTOM_LIFECYCLE_TASK_ERROR_MSG, task.getName())); } } }); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy