diff --git a/README.adoc b/README.adoc index 7a5988e..b58212f 100644 --- a/README.adoc +++ b/README.adoc @@ -66,6 +66,7 @@ The following properties can be specified in the `jandex` task configuration | processDefaultFileSet | jandex-process-default-file-set | jandex.process.default.file.set | boolean | true | includeInJar | jandex-includein-jar | jandex.include.in.jar | boolean | true | indexName | jandex-index-name | jandex.index.name | String | jandex.idx +| indexVersion | jandex-index-version | jandex.index.version | Integer | | destination | | | File | build/jandex/jandex.idx | sources | | | ConfigurableFileCollection | sourceSets.main.output.classesDirs |=== diff --git a/build.gradle b/build.gradle index f949366..4480274 100644 --- a/build.gradle +++ b/build.gradle @@ -59,6 +59,7 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2' testImplementation 'org.assertj:assertj-core:3.11.1' + testImplementation "io.smallrye:jandex:$jandexVersion" testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' } diff --git a/src/functionalTest/groovy/org/kordamp/gradle/plugin/jandex/JandexPluginPluginFunctionalTest.groovy b/src/functionalTest/groovy/org/kordamp/gradle/plugin/jandex/JandexPluginPluginFunctionalTest.groovy index 9f8ab8f..6b5240b 100644 --- a/src/functionalTest/groovy/org/kordamp/gradle/plugin/jandex/JandexPluginPluginFunctionalTest.groovy +++ b/src/functionalTest/groovy/org/kordamp/gradle/plugin/jandex/JandexPluginPluginFunctionalTest.groovy @@ -18,9 +18,9 @@ package org.kordamp.gradle.plugin.jandex import org.assertj.core.api.Assertions -import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.TaskOutcome +import org.jboss.jandex.IndexReader import org.junit.jupiter.api.Test class JandexPluginPluginFunctionalTest { @@ -105,6 +105,121 @@ public class AClass { Assertions.assertThat(result4.task(':jandex').outcome).isEqualTo(TaskOutcome.UP_TO_DATE); } + @Test + void testIndexVersion() { + projectDir = new File("build/functionalTestFixture/indexVersion_${System.currentTimeMillis()}") + projectDir.mkdirs() + + def indexFile = new File(projectDir, 'build/resources/main/META-INF/jandex.idx') + Assertions.assertThat(indexFile).doesNotExist(); + + settingsFile.text = "" + buildFile.text = """ +plugins { + id 'org.kordamp.gradle.jandex' +} + +repositories { + mavenCentral() +} + +jandex { + indexVersion = 2 +} +""" + + sourceFileAClass.text = """ +package com.sample; + +public class AClass { + public void sayHi() { + System.out.println("hi"); + } +} +""" + def runner1 = createRunner() + def result1 = runner1.build() + Assertions.assertThat(result1.task(':jandex').outcome).isEqualTo(TaskOutcome.SUCCESS); + Assertions.assertThat(indexFile).exists() + Assertions.assertThat(new IndexReader(indexFile.newInputStream()).indexVersion).isEqualTo(2) + def content1 = indexFile.getText("UTF-8") + Assertions.assertThat(content1).contains("AClass") + + //Re run without any changes, to be sure the task is up-to-date: + def runner2 = createRunner() + def result2 = runner2.build() + Assertions.assertThat(result2.task(':jandex').outcome).isEqualTo(TaskOutcome.UP_TO_DATE); + + //Modify index version, and verify jandex was executed again: + buildFile.text = """ +plugins { + id 'org.kordamp.gradle.jandex' +} + +repositories { + mavenCentral() +} + +jandex { + indexVersion = 9 +} +""" + def runner3 = createRunner() + def result3 = runner3.build() + Assertions.assertThat(result3.task(':jandex').outcome).isEqualTo(TaskOutcome.SUCCESS); + Assertions.assertThat(indexFile).exists() + Assertions.assertThat(new IndexReader(indexFile.newInputStream()).indexVersion).isEqualTo(9) + def content3 = indexFile.getText("UTF-8") + Assertions.assertThat(content3) + .isNotEqualTo(content1) + .contains("AClass") + .contains("sayHi") + + //Re run without any changes, to be sure the task is up-to-date: + def runner4 = createRunner() + def result4 = runner4.build() + Assertions.assertThat(result4.task(':jandex').outcome).isEqualTo(TaskOutcome.UP_TO_DATE) + } + + @Test + void testUnsupportedIndexVersion() { + projectDir = new File("build/functionalTestFixture/unsupportedIndexVersion_${System.currentTimeMillis()}") + projectDir.mkdirs() + + def indexFile = new File(projectDir, 'build/resources/main/META-INF/jandex.idx') + Assertions.assertThat(indexFile).doesNotExist() + + settingsFile.text = "" + buildFile.text = """ +plugins { + id 'org.kordamp.gradle.jandex' +} + +repositories { + mavenCentral() +} + +jandex { + indexVersion = 1000 +} +""" + + sourceFileAClass.text = """ +package com.sample; + +public class AClass { + public void sayHi() { + System.out.println("hi"); + } +} +""" + def result = createRunner().buildAndFail() + Assertions.assertThat(result .task(':jandex').outcome).isEqualTo(TaskOutcome.FAILED); + Assertions.assertThat(result .output) + .contains("org.jboss.jandex.UnsupportedVersion") + .contains("Can't write index version 1000; this IndexWriter only supports index versions") + } + def createRunner() { def runner = GradleRunner.create() runner.forwardOutput() diff --git a/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexExtension.groovy b/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexExtension.groovy index aa2fb39..3afd8c2 100644 --- a/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexExtension.groovy +++ b/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexExtension.groovy @@ -26,4 +26,6 @@ import org.gradle.api.provider.Property @CompileStatic interface JandexExtension { Property getVersion() + + Property getIndexVersion() } diff --git a/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexPlugin.groovy b/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexPlugin.groovy index 759afb2..eca619e 100644 --- a/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexPlugin.groovy +++ b/src/main/groovy/org/kordamp/gradle/plugin/jandex/JandexPlugin.groovy @@ -76,6 +76,7 @@ class JandexPlugin implements Plugin { if (t.resolvedProcessDefaultFileSet.get()) { t.inputs.files(t.sourceSets.findByName('main').output.classesDirs*.absolutePath.flatten()) } + t.indexVersion = jandexExtension.indexVersion.getOrNull() } }) diff --git a/src/main/groovy/org/kordamp/gradle/plugin/jandex/internal/JandexExtensionImpl.groovy b/src/main/groovy/org/kordamp/gradle/plugin/jandex/internal/JandexExtensionImpl.groovy index a05bf81..27181f2 100644 --- a/src/main/groovy/org/kordamp/gradle/plugin/jandex/internal/JandexExtensionImpl.groovy +++ b/src/main/groovy/org/kordamp/gradle/plugin/jandex/internal/JandexExtensionImpl.groovy @@ -30,9 +30,11 @@ import javax.inject.Inject @CompileStatic class JandexExtensionImpl implements JandexExtension { final Property version + final Property indexVersion @Inject JandexExtensionImpl(ObjectFactory objects) { version = objects.property(String).convention(DefaultVersions.INSTANCE.jandexVersion) + indexVersion = objects.property(Integer).unset() } } \ No newline at end of file diff --git a/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexTask.groovy b/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexTask.groovy index a16e0ea..caae572 100644 --- a/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexTask.groovy +++ b/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexTask.groovy @@ -36,6 +36,7 @@ import org.gradle.api.tasks.Copy import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.TaskAction @@ -45,7 +46,9 @@ import org.gradle.workers.ClassLoaderWorkerSpec import org.gradle.workers.WorkQueue import org.gradle.workers.WorkerExecutor import org.kordamp.gradle.property.BooleanState +import org.kordamp.gradle.property.IntegerState import org.kordamp.gradle.property.SimpleBooleanState +import org.kordamp.gradle.property.SimpleIntegerState import org.kordamp.gradle.property.SimpleStringState import org.kordamp.gradle.property.StringState @@ -59,6 +62,7 @@ class JandexTask extends DefaultTask { private final BooleanState processDefaultFileSet private final BooleanState includeInJar private final StringState indexName + private final IntegerState indexVersion private final WorkerExecutor workerExecutor @Classpath @@ -88,6 +92,7 @@ class JandexTask extends DefaultTask { processDefaultFileSet = SimpleBooleanState.of(this, 'jandex.process.default.file.set', true) includeInJar = SimpleBooleanState.of(this, 'jandex.include.in.jar', true) indexName = SimpleStringState.of(this, 'jandex.index.name', 'jandex.idx') + indexVersion = SimpleIntegerState.of(this, 'jandex.index.version') sources = objects.fileCollection() destination = objects.fileProperty() @@ -112,6 +117,9 @@ class JandexTask extends DefaultTask { @Option(option = 'jandex-index-name', description = "The name of the index file. Defaults to jandex.idx") void setIndexName(String value) { indexName.property.set(value) } + @Option(option = 'jandex-index-version', description = "The version of the index file. Defaults to the latest version supported by the invoked Jandex version") + void setIndexVersion(Integer value) { indexVersion.property.set(value) } + @Internal Property getProcessDefaultFileSet() { processDefaultFileSet.property } @@ -127,6 +135,10 @@ class JandexTask extends DefaultTask { @Internal Property getIndexName() { indexName.property } + @Input + @Optional + Property getIndexVersion() { indexVersion.property } + @Input Provider getResolvedIndexName() { indexName.provider } @@ -145,6 +157,7 @@ class JandexTask extends DefaultTask { void execute(JandexWorkParameters parameters) { parameters.sources.set(resolveSources()) parameters.destination.set(destination) + parameters.indexVersion.set(indexVersion) } }) } diff --git a/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexWorkAction.groovy b/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexWorkAction.groovy index 20c863d..62e6d24 100644 --- a/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexWorkAction.groovy +++ b/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexWorkAction.groovy @@ -62,12 +62,17 @@ abstract class JandexWorkAction implements WorkAction { File destination = parameters.destination.asFile.get() FileOutputStream output = new FileOutputStream(destination) + Integer indexVersion = parameters.indexVersion.getOrNull() try { destination.parentFile.mkdirs() IndexWriter writer = new IndexWriter(output) Index index = indexer.complete() - writer.write(index) + if (indexVersion == null || indexVersion == 0) { + writer.write(index) + } else { + writer.write(index, indexVersion) + } logger.info('Index has been written to ' + destination.absolutePath) } finally { output?.close() diff --git a/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexWorkParameters.groovy b/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexWorkParameters.groovy index aaba830..44a76d7 100644 --- a/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexWorkParameters.groovy +++ b/src/main/groovy/org/kordamp/gradle/plugin/jandex/tasks/JandexWorkParameters.groovy @@ -20,6 +20,7 @@ package org.kordamp.gradle.plugin.jandex.tasks import groovy.transform.CompileStatic import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.ListProperty +import org.gradle.api.provider.Property import org.gradle.workers.WorkParameters /** @@ -30,4 +31,6 @@ interface JandexWorkParameters extends WorkParameters { ListProperty getSources() RegularFileProperty getDestination() + + Property getIndexVersion() } \ No newline at end of file