|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.jooby.run; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.LinkedHashSet; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.gradle.api.Plugin; |
| 28 | +import org.gradle.api.Project; |
| 29 | +import org.gradle.api.Task; |
| 30 | +import org.gradle.api.internal.ConventionMapping; |
| 31 | +import org.gradle.api.invocation.Gradle; |
| 32 | +import org.gradle.api.plugins.Convention; |
| 33 | +import org.gradle.api.plugins.JavaPluginConvention; |
| 34 | +import org.gradle.api.tasks.SourceSet; |
| 35 | + |
| 36 | +public class JoobyPlugin implements Plugin<Project> { |
| 37 | + |
| 38 | + @Override |
| 39 | + public void apply(final Project project) { |
| 40 | + JoobyPluginConvention jettyConvention = new JoobyPluginConvention(); |
| 41 | + Convention convention = project.getConvention(); |
| 42 | + convention.getPlugins().put("jooby", jettyConvention); |
| 43 | + |
| 44 | + configureJoobyRun(project); |
| 45 | + } |
| 46 | + |
| 47 | + private void configureJoobyRun(final Project project) { |
| 48 | + project.getTasks() |
| 49 | + .withType(JoobyTask.class, joobyRun -> { |
| 50 | + ConventionMapping mapping = joobyRun.getConventionMapping(); |
| 51 | + mapping.map("classpath", () -> { |
| 52 | + SourceSet sourceSet = getJavaConvention(project).getSourceSets() |
| 53 | + .getByName(SourceSet.MAIN_SOURCE_SET_NAME); |
| 54 | + |
| 55 | + Set<File> cp = new LinkedHashSet<>(); |
| 56 | + // conf & public |
| 57 | + sourceSet.getResources().getSrcDirs().forEach(cp::add); |
| 58 | + // classes/main, resources/main + jars |
| 59 | + sourceSet.getRuntimeClasspath().getFiles().forEach(cp::add); |
| 60 | + |
| 61 | + return cp; |
| 62 | + }); |
| 63 | + |
| 64 | + mapping.map("mainClassName", () -> project.getProperties().get("mainClassName")); |
| 65 | + |
| 66 | + Gradle gradle = project.getGradle(); |
| 67 | + mapping.map("block", () -> !gradle.getStartParameter().isContinuous()); |
| 68 | + mapping.map("logLevel", () -> gradle.getStartParameter().getLogLevel().name()); |
| 69 | + }); |
| 70 | + |
| 71 | + Map<String, Object> options = new HashMap<>(); |
| 72 | + options.put(Task.TASK_TYPE, JoobyTask.class); |
| 73 | + options.put(Task.TASK_DEPENDS_ON, new String[]{"processResources", "compileJava" }); |
| 74 | + options.put(Task.TASK_NAME, "joobyRun"); |
| 75 | + options.put(Task.TASK_GROUP, "jooby"); |
| 76 | + project.getTasks().create(options); |
| 77 | + } |
| 78 | + |
| 79 | + public JavaPluginConvention getJavaConvention(final Project project) { |
| 80 | + return project.getConvention().getPlugin(JavaPluginConvention.class); |
| 81 | + } |
| 82 | +} |
0 commit comments