Merge pull request #9542 from srzamfir/feature/BAEL-4090_Finding_Unused_Gradle_Dependencies

Feature/bael 4090 finding unused gradle dependencies
This commit is contained in:
Eric Martin 2020-06-28 10:35:44 -05:00 committed by GitHub
commit 2e3b36bb56
10 changed files with 118 additions and 50 deletions

2
gradle-5/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
**/build/**
/build/

View File

@ -1,54 +1,21 @@
plugins {
id "application"
}
apply plugin :"java"
description = "Java MainClass execution examples"
group = "com.baeldung"
version = "0.0.1"
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
ext {
javaMainClass = "com.baeldung.gradle.exec.MainClass"
plugins{
id "nebula.lint" version "16.9.0"
}
jar {
manifest {
attributes(
"Main-Class": javaMainClass
)
description = "Gradle 5 root project"
allprojects {
apply plugin :"java"
apply plugin :"nebula.lint"
gradleLint {
rules=['unused-dependency']
reportFormat = 'text'
}
}
group = "com.baeldung"
version = "0.0.1"
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
application {
mainClassName = javaMainClass
}
task runWithJavaExec(type: JavaExec) {
group = "Execution"
description = "Run the main class with JavaExecTask"
classpath = sourceSets.main.runtimeClasspath
main = javaMainClass
}
task runWithExec(type: Exec) {
dependsOn build
group = "Execution"
description = "Run the main class with ExecTask"
commandLine "java", "-classpath", sourceSets.main.runtimeClasspath.getAsPath(), javaMainClass
}
task runWithExecJarExecutable(type: Exec) {
dependsOn jar
group = "Execution"
description = "Run the output executable jar with ExecTask"
commandLine "java", "-jar", jar.archiveFile.get()
}
task runWithExecJarOnClassPath(type: Exec) {
dependsOn jar
group = "Execution"
description = "Run the mainClass from the output jar in classpath with ExecTask"
commandLine "java", "-classpath", jar.archiveFile.get() , javaMainClass
repositories {
jcenter()
}
}

View File

@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip

1
gradle-5/java-exec/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build/

View File

@ -0,0 +1,47 @@
apply plugin: "application"
description = "Java MainClass execution examples"
ext {
javaMainClass = "com.baeldung.gradle.exec.MainClass"
}
jar {
manifest {
attributes(
"Main-Class": javaMainClass
)
}
}
application {
mainClassName = javaMainClass
}
task runWithJavaExec(type: JavaExec) {
group = "Execution"
description = "Run the main class with JavaExecTask"
classpath = sourceSets.main.runtimeClasspath
main = javaMainClass
}
task runWithExec(type: Exec) {
dependsOn build
group = "Execution"
description = "Run the main class with ExecTask"
commandLine "java", "-classpath", sourceSets.main.runtimeClasspath.getAsPath(), javaMainClass
}
task runWithExecJarExecutable(type: Exec) {
dependsOn jar
group = "Execution"
description = "Run the output executable jar with ExecTask"
commandLine "java", "-jar", jar.archiveFile.get()
}
task runWithExecJarOnClassPath(type: Exec) {
dependsOn jar
group = "Execution"
description = "Run the mainClass from the output jar in classpath with ExecTask"
commandLine "java", "-classpath", jar.archiveFile.get() , javaMainClass
}

3
gradle-5/settings.gradle Normal file
View File

@ -0,0 +1,3 @@
rootProject.name='gradle-5-articles'
include 'java-exec'
include 'unused-dependencies'

View File

@ -0,0 +1 @@
/build/

View File

@ -0,0 +1,8 @@
description = "Gradle Unused Dependencies example"
dependencies {
implementation('com.google.guava:guava:29.0-jre')
implementation('org.apache.httpcomponents:httpclient:4.5.12')
testImplementation('junit:junit:4.12')
}

View File

@ -0,0 +1,39 @@
package com.baeldung.unused;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.http.ssl.SSLContextBuilder;
import com.google.common.collect.ImmutableList;
public class UnusedDependencies {
public static void main(String[] args) {
System.out.println("Hello world");
useGuava();
useHttpCore();
useHttpClientWithReflection();
}
private static void useGuava() {
List<String> list = ImmutableList.of("Baledung", "is", "cool");
System.out.println(list.stream()
.collect(Collectors.joining(" ")));
}
private static void useHttpCore() {
SSLContextBuilder.create();
}
private static void useHttpClientWithReflection() {
try {
Class<?> httpBuilder = Class.forName("org.apache.http.impl.client.HttpClientBuilder");
Method create = httpBuilder.getMethod("create", null);
create.invoke(httpBuilder, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}