01 /*
02 * SPDX-License-Identifier: Apache-2.0
03 *
04 * Copyright 2019-2021 Andres Almiray.
05 *
06 * Licensed under the Apache License, Version 2.0 (the "License");
07 * you may not use this file except in compliance with the License.
08 * You may obtain a copy of the License at
09 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.kordamp.gradle.plugin.yguard
19
20 import groovy.transform.CompileDynamic
21 import groovy.transform.CompileStatic
22 import org.gradle.api.Action
23 import org.gradle.api.Plugin
24 import org.gradle.api.Project
25 import org.gradle.api.artifacts.Configuration
26 import org.gradle.api.plugins.AppliedPlugin
27 import org.gradle.api.plugins.BasePlugin
28 import org.gradle.api.tasks.TaskProvider
29 import org.gradle.jvm.tasks.Jar
30 import org.kordamp.gradle.plugin.yguard.tasks.YGuardGradleTask
31
32 /**
33 *
34 * @author Andres Almiray
35 * @since 0.1.0
36 */
37 @CompileStatic
38 class YGuardPlugin implements Plugin<Project> {
39 static final String YGUARD = 'yguard'
40
41 @Override
42 void apply(Project project) {
43 Banner.display(project)
44
45 project.pluginManager.withPlugin('java-base', new Action<AppliedPlugin>() {
46 @Override
47 void execute(AppliedPlugin p) {
48 YGuardExtension extension = project.extensions.create(YGUARD, YGuardExtension, project.objects)
49 Configuration configuration = project.configurations.maybeCreate(YGUARD)
50
51 addDependenciesAfterEvaluate(project, extension)
52
53 String taskName = 'yguardMain'
54 TaskProvider<Jar> jar = project.tasks.named('jar', Jar)
55
56 project.tasks.register(taskName, YGuardGradleTask, new Action<YGuardGradleTask>() {
57 @Override
58 @CompileDynamic
59 void execute(YGuardGradleTask t) {
60 t.dependsOn(jar)
61 t.group = BasePlugin.BUILD_GROUP
62 t.description = 'Obfuscates the jar archive containing the main classes'
63 t.classpath.from(configuration)
64 t.externalClasses.from(
65 project.configurations.findByName('compileClasspath'),
66 project.configurations.findByName('runtimeClasspath'))
67 t.inputJars.from(project.files(jar.get().archiveFile.get()))
68 t.outputDirectory.set(project.layout.buildDirectory.dir('yguard/main').get())
69 }
70 })
71
72 jar.configure(new Action<Jar>() {
73 @Override
74 void execute(Jar t) {
75 t.finalizedBy(project.tasks.named(taskName))
76 }
77 })
78 }
79 })
80 }
81
82 private void addDependenciesAfterEvaluate(Project project,
83 YGuardExtension extension) {
84 project.afterEvaluate {
85 if (extension.includeDefaultRepositories) {
86 project.repositories.mavenCentral()
87 }
88
89 project.dependencies.add('yguard', "com.yworks:yguard:${extension.toolVersion.get()}")
90 }
91 }
92 }
|