Skip to content

Commit 4f1098e

Browse files
committed
Add exportAllPackagesExcept
1 parent b31b444 commit 4f1098e

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Extra Java Module Info Gradle Plugin - Changelog
22

33
## Version 1.14
4-
* [Fixed] [#179](https://github.com/gradlex-org/extra-java-module-info/issues/179) - Combine 'preserveExisting' and 'exports'
5-
* [Fixed] [#219](https://github.com/gradlex-org/extra-java-module-info/issues/219) - Combine 'preserveExisting' and 'removePackage'
4+
* [New] [#207](https://github.com/gradlex-org/extra-java-module-info/issues/207) - Add 'exportAllPackagesExcept("org.exception", ...)' (Thanks [Tim Hurman](https://github.com/timhamoni) for contributing)
5+
* [Fixed] [#179](https://github.com/gradlex-org/extra-java-module-info/issues/179) - Combine 'preserveExisting' and 'exports' (Thanks to [JabRef](https://github.com/JabRef/jabref) for providing a use case)
6+
* [Fixed] [#219](https://github.com/gradlex-org/extra-java-module-info/issues/219) - Combine 'preserveExisting' and 'removePackage' (Thanks to [stewue](https://github.com/stewue) for providing a use case)
67
* [Fixed] [#223](https://github.com/gradlex-org/extra-java-module-info/issues/223) - Combine 'preserveExisting' and 'ignoreServiceProvider'
78
* [Fixed] [#227](https://github.com/gradlex-org/extra-java-module-info/issues/227) - Stable order for the mergeJar path input
89

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ extraJavaModuleInfo {
7373
// skipLocalJars = true
7474
module("commons-beanutils:commons-beanutils", "org.apache.commons.beanutils") {
7575
exports("org.apache.commons.beanutils")
76-
// or granuarly allowing access to a package by specific modules
76+
// or granularly allowing access to a package by specific modules
7777
// exports("org.apache.commons.beanutils",
7878
// "org.mycompany.server", "org.mycompany.client")
7979
// or simply export all packages
8080
// exportAllPackages()
81+
// or export all packages except specific named ones
82+
// exportAllPackagesExcept("org.mycompany.notgood1", "org.mycompany.notgood2")
8183

8284
requiresTransitive("org.apache.commons.logging")
8385
requires("java.sql")

src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoTransform.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,17 @@ private void addModuleDescriptor(File originalJar, File moduleJar, ModuleInfo mo
337337
providers,
338338
packages);
339339
mergeJars(moduleInfo, outputStream, providers, packages);
340+
if (moduleInfo.exportAllPackages) {
341+
moduleInfo.exportAllPackagesExceptions.forEach(it -> packages.remove(packageToPath(it)));
342+
} else {
343+
packages.clear();
344+
}
340345
outputStream.putNextEntry(newReproducibleEntry("module-info.class"));
341346
outputStream.write(addModuleInfo(
342347
moduleInfo,
343348
providers,
344349
versionFromFilePath(originalJar.toPath()),
345-
moduleInfo.exportAllPackages ? packages : emptySet(),
350+
packages,
346351
moduleInfo.getRemovedPackages(),
347352
moduleInfo.ignoreServiceProviders,
348353
existingModuleInfo));

src/main/java/org/gradlex/javamodule/moduleinfo/ModuleInfo.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
package org.gradlex.javamodule.moduleinfo;
33

44
import java.util.Arrays;
5+
import java.util.Collections;
56
import java.util.LinkedHashMap;
67
import java.util.LinkedHashSet;
8+
import java.util.List;
79
import java.util.Map;
810
import java.util.Set;
911
import org.gradle.api.model.ObjectFactory;
10-
import org.jspecify.annotations.NullMarked;
1112
import org.jspecify.annotations.Nullable;
1213

1314
/**
1415
* Data class to hold the information that should be added as module-info.class to an existing Jar file.
1516
*/
16-
@NullMarked
17+
@SuppressWarnings("unused")
1718
public class ModuleInfo extends ModuleSpec {
1819

1920
@Nullable
@@ -28,6 +29,7 @@ public class ModuleInfo extends ModuleSpec {
2829
final Set<String> requiresStaticTransitive = new LinkedHashSet<>();
2930
final Map<String, Set<String>> ignoreServiceProviders = new LinkedHashMap<>();
3031
final Set<String> uses = new LinkedHashSet<>();
32+
final Set<String> exportAllPackagesExceptions = new LinkedHashSet<>();
3133

3234
boolean exportAllPackages;
3335
boolean requireAllDefinedDependencies;
@@ -120,7 +122,24 @@ public String getModuleVersion() {
120122
* Automatically export all packages of the Jar. Can be used instead of individual 'exports()' statements.
121123
*/
122124
public void exportAllPackages() {
125+
exportAllPackagesExcept(Collections.emptyList());
126+
}
127+
128+
/**
129+
* Automatically export all packages of the Jar. Can be used instead of individual 'exports()' statements.
130+
* @param exceptions A list of packages not to export
131+
*/
132+
public void exportAllPackagesExcept(String... exceptions) {
133+
exportAllPackagesExcept(Arrays.asList(exceptions));
134+
}
135+
136+
/**
137+
* Automatically export all packages of the Jar. Can be used instead of individual 'exports()' statements.
138+
* @param exceptions A list of packages not to export
139+
*/
140+
public void exportAllPackagesExcept(List<String> exceptions) {
123141
this.exportAllPackages = true;
142+
exportAllPackagesExceptions.addAll(exceptions);
124143
}
125144

126145
/**

src/test/groovy/org/gradlex/javamodule/moduleinfo/test/EdgeCasesFunctionalTest.groovy

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,53 @@ class EdgeCasesFunctionalTest extends Specification {
177177
run().task(':run').outcome == TaskOutcome.SUCCESS
178178
}
179179

180+
def "can automatically export all packages except specified of a legacy Jar"() {
181+
given:
182+
file("src/main/java/org/gradle/sample/app/Main.java") << """
183+
package org.gradle.sample.app;
184+
185+
import javax.json.JsonString;
186+
import javax.json.JsonValue;
187+
188+
public class Main {
189+
public static void main(String[] args) {
190+
JsonString jsonString = new JsonString() {
191+
public boolean equals(Object obj) { return false; }
192+
public CharSequence getChars() { return null; }
193+
public String getString() { return null; }
194+
public int hashCode() { return 0; }
195+
public JsonValue.ValueType getValueType() { return null; }
196+
};
197+
}
198+
}
199+
"""
200+
201+
file("src/main/java/module-info.java") << """
202+
module org.gradle.sample.app {
203+
exports org.gradle.sample.app;
204+
205+
requires java.json;
206+
}
207+
"""
208+
buildFile << """
209+
dependencies {
210+
implementation("org.glassfish:jakarta.json:1.1.6")
211+
implementation("jakarta.json:jakarta.json-api:1.1.6")
212+
}
213+
214+
extraJavaModuleInfo {
215+
module("org.glassfish:jakarta.json", "java.json") {
216+
// exportAllPackages()
217+
exportAllPackagesExcept("javax.json", "javax.json.spi", "javax.json.stream")
218+
}
219+
}
220+
"""
221+
222+
expect:
223+
def result = failRun()
224+
result.output.contains("error: package javax.json is not visible")
225+
}
226+
180227
def "deriveAutomaticModuleNamesFromFileNames produces a build time error for invalid module names"() {
181228
given:
182229
buildFile << """

0 commit comments

Comments
 (0)