Skip to content

Commit

Permalink
Exclude slf4j-api from grgit
Browse files Browse the repository at this point in the history
To prevent the plugin from shipping its own slf4j-api jar,
which is improperly relocated and hence fails to bind to the gradle
provided sfl4j implementation, this commit excludes the slf4j-api from
grgit.

The commit additionally contains an integration test that tests the
shadowed plugin jar to ensure slf4j can properly bind.
  • Loading branch information
lynxplay committed Mar 2, 2022
1 parent 4723749 commit e831e2f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
21 changes: 20 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,26 @@ repositories {
//}
}

def integrationTest = sourceSets.create("integrationTest")

dependencies {
shadow gradleApi()
shadow localGroovy()
implementation 'org.ajoberstar.grgit:grgit-core:4.1.1'
implementation('org.ajoberstar.grgit:grgit-core:4.1.1') {
exclude(group: 'org.slf4j')
}
// for debugging
//implementation 'org.gradle:gradle-language-jvm:5.6.2'
//implementation 'org.gradle:gradle-language-java:5.6.2'


testImplementation 'junit:junit:4.12'
testImplementation gradleTestKit()

integrationTestImplementation 'junit:junit:4.12'
integrationTestImplementation gradleTestKit()
integrationTestImplementation project
integrationTestImplementation sourceSets.test.output // Include test files for git project utils
}

task relocateShadowJar(type: ConfigureShadowRelocation) {
Expand Down Expand Up @@ -85,6 +94,16 @@ test {
}
}

tasks.register("integrationTest", Test) {
dependsOn tasks.shadowJar

description = 'Runs the integration tests.'
group = "verification"
testClassesDirs = integrationTest.output.classesDirs
classpath = integrationTest.runtimeClasspath
it.systemProperty "integration.plugin.path", tasks.shadowJar.outputs.getFiles().asPath // Pass java system property indicating integration plugin path
}

tasks.register("sourceJar", Jar) {
classifier = "sources"
from sourceSets.main.resources, sourceSets.main.groovy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.gorylenko

import com.gorylenko.properties.GitRepositoryBuilder
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

import static org.junit.Assert.*

class SLF4JBindingPostShadowTest {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder()

@Test
void testSFL4JBindingPostShadow() {
def projectDir = temporaryFolder.newFolder()

def integrationPluginPath = System.properties.get("integration.plugin.path") // Fetch integration jar from shadowJar output
assertNotNull("integration.plugin.path was null", integrationPluginPath)

GitRepositoryBuilder.setupProjectDir(projectDir, { gitRepoBuilder ->
// commit 1 new file "hello.txt"
gitRepoBuilder.commitFile("hello.txt", "Hello", "Added hello.txt")
})

new File(projectDir, "settings.gradle") << ""
new File(projectDir, "build.gradle") << """
plugins {
id('com.gorylenko.gradle-git-properties')
}
""".stripIndent()

def runner = GradleRunner.create()
.withPluginClasspath(Arrays.asList(new File(integrationPluginPath as String))) // Use integration plugin jar
.withArguments("generateGitProperties")
.withProjectDir(projectDir)

def result = runner.build()

assertEquals(TaskOutcome.SUCCESS, result.task(":generateGitProperties").outcome)
assertFalse("Project improperly shadowed slf4j-api!", result.output.contains("org.slf4j.impl.StaticLoggerBinder"))
}

}

0 comments on commit e831e2f

Please sign in to comment.