You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "ppkarwasz (via GitHub)" <gi...@apache.org> on 2024/09/16 08:25:04 UTC
[PR] Replace BeanShell with Groovy [logging-parent]
ppkarwasz opened a new pull request, #241:
URL: https://github.com/apache/logging-parent/pull/241
This replaces the BeanShell script used to create source and binary distribution with a Groovy script.
The main motivations for the change are:
- While the [BeanShell project](https://github.com/beanshell/beanshell) is still active, the `2.x` branch has no releases in Maven Central since 2015. The `3.x` branch has only snapshots.
[Apache Groovy](https://groovy-lang.org/) on the other hand is a very active project.
- The [`beanshell-maven-plugin`](https://github.com/genthaler/beanshell-maven-plugin) seems dormant since 2017 or does not have enough users to report bugs against it.
The [`gmaven-plugin`](https://github.com/groovy/GMavenPlus) on the other hand is relatively active and supported by the Apache Groovy team.
Closes #196.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace BeanShell with Groovy [logging-parent]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #241:
URL: https://github.com/apache/logging-parent/pull/241#discussion_r1763184142
##########
pom.xml:
##########
@@ -1154,131 +1170,128 @@
<execution>
<id>create-distribution</id>
<goals>
- <goal>run</goal>
+ <goal>execute</goal>
</goals>
<configuration>
- <script><![CDATA[import java.io.*;
- import java.nio.file.*;
- import java.util.*;
- import java.util.function.*;
- import java.util.stream.*;
- import java.util.zip.*;
-
- import org.eclipse.jgit.dircache.*;
- import org.eclipse.jgit.lib.Repository;
- import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-
- String outputTimestamp = project.getProperties().getProperty("project.build.outputTimestamp");
- long timestampMillis = java.time.Instant.parse(outputTimestamp).toEpochMilli();
- zip(String zipFileName, Map pathByFile) {
- OutputStream outputStream = new FileOutputStream(zipFileName);
- ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
- try {
- for (String file : pathByFile.keySet()) {
- Path path = pathByFile.get(file);
- // Skip directories, which is tracked by Git only when it is a symlink
- if (Files.isDirectory(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- continue;
- }
- try {
- ZipEntry zipEntry = new ZipEntry(file);
- zipEntry.setTime(timestampMillis);
- zipOutputStream.putNextEntry(zipEntry);
- zipOutputStream.write(Files.readAllBytes(path));
- zipOutputStream.closeEntry();
- } catch (Exception error) {
- String message = String.format(
- "failed processing file `%s` at path `%s`",
- new Object[]{file, path});
- throw new RuntimeException(message, error);
- }
- }
- } catch (Exception error) {
- String message = String.format("failed creating ZIP archive `%s`", new Object[]{zipFileName});
- Exception extendedError = new RuntimeException(message, error);
- // Supplement diagnostics
- extendedError.printStackTrace(System.err);
- throw extendedError;
- } finally {
- zipOutputStream.close();
- }
- }
-
- // Find Git-tracked files
- SortedMap pathByFile = new TreeMap();
- Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build();
- DirCache repoCache = repo.readDirCache();
- String repoDirectoryParent = repo.getDirectory().getParent();
- for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
- DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex);
- String repoCacheEntryPath = repoCacheEntry.getPathString();
- pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath());
- }
-
- // Create the source distribution using Git-tracked files
- zip("target/src.zip", pathByFile);
- System.out.format("Generated the source distribution (`src.zip`) containing %d files.%n", new Object[]{pathByFile.size()});
-
- // Short-circuit if there is no binary distribution expected
- if (${attachmentCount} == 0) {
- return;
- }
-
- // Find auxiliary files that will go into the binary distribution
- SortedMap pathByFile = new TreeMap();
- pathByFile.put("RELEASE-NOTES.adoc", new File("${project.build.directory}/generated-site/antora/modules/ROOT/pages/_release-notes/${project.version}.adoc").toPath());
- pathByFile.put("README.adoc", new File("README.adoc").toPath());
- pathByFile.put("NOTICE.txt", new File("NOTICE.txt").toPath());
- pathByFile.put("LICENSE.txt", new File("LICENSE.txt").toPath());
-
- // Find attachments that will go into the binary distribution
- Path projectDir = Paths.get("${maven.multiModuleProjectDirectory}", new String[0]);
- String attachmentFilepathPattern = "${attachmentFilepathPattern}";
- System.out.format("Locating attachments matching the provided pattern: `%s`%n", new Object[] {attachmentFilepathPattern});
- SortedMap attachmentPathByFile = new TreeMap();
- Stream paths = Files.walk(new File(repoDirectoryParent).toPath(), 32, /* required for BSH method resolution: */ new FileVisitOption[0]);
- try {
- paths.forEach(new Consumer() {
- public void accept(Path path) {
- if (Files.isRegularFile(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- String relativePath = projectDir.relativize(path).toString();
- if (relativePath.matches(attachmentFilepathPattern)) {
- attachmentPathByFile.put(path.getFileName().toString(), path);
- }
- }
- }
- });
- } catch (Exception error) {
- // Supplement diagnostics
- error.printStackTrace(System.err);
- throw error;
- } finally {
- paths.close();
- }
-
- // Check if no attachments were found
- if (${attachmentCount} != attachmentPathByFile.size()) {
- System.err.println("Attachments:");
- int[] i = {0};
- attachmentPathByFile.values().forEach(new Consumer() {
- public void accept(Path path) {
- System.err.format(" [%2d] %s%n", new Object[]{++i[0], path});
- }
- });
- System.err.format(
- "Error: Was expecting %d attachments, found %d!%n",
- new Object[]{${attachmentCount}, attachmentPathByFile.size()});
- System.err.println("Tip: Have you already executed the Maven `package` goal?");
- System.exit(1);
- }
-
- // Create the binary distribution
- pathByFile.putAll(attachmentPathByFile);
- zip("target/bin.zip", pathByFile);
- System.out.format("Generated the binary distribution (`bin.zip`) containing following %d files:%n", new Object[] {pathByFile.size()});
- for (String file : pathByFile.keySet()) {
- System.out.println("-> " + file);
- }]]></script>
+ <scripts>
+ <script><![CDATA[import java.io.*;
+ import java.nio.file.*;
+ import java.util.*;
+ import java.util.function.*;
+ import java.util.regex.*;
+ import java.util.stream.*;
+ import java.util.zip.*;
+ import java.time.Instant;
+
+ import org.eclipse.jgit.dircache.*;
+ import org.eclipse.jgit.lib.Repository;
+ import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+ long timestampMillis = Instant.parse(project.properties['project.build.outputTimestamp']).toEpochMilli()
+ def getProperty = { String name ->
+ return session.userProperties[name] ?: session.systemProperties[name]
+ }
Review Comment:
I prefer not to inject the value of a property, since it can break the script. For example `"${attachmentFilepathPattern}"` will not compile if the value is `.*\.jar`.
I refactored the script to bind all the necessary string parameters in https://github.com/apache/logging-parent/pull/241/commits/978499680344d8b77833bcc6c4a70bd7650f0397.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace BeanShell with Groovy [logging-parent]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz merged PR #241:
URL: https://github.com/apache/logging-parent/pull/241
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace BeanShell with Groovy [logging-parent]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #241:
URL: https://github.com/apache/logging-parent/pull/241#discussion_r1763101954
##########
pom.xml:
##########
@@ -1154,131 +1170,128 @@
<execution>
<id>create-distribution</id>
<goals>
- <goal>run</goal>
+ <goal>execute</goal>
</goals>
<configuration>
- <script><![CDATA[import java.io.*;
- import java.nio.file.*;
- import java.util.*;
- import java.util.function.*;
- import java.util.stream.*;
- import java.util.zip.*;
-
- import org.eclipse.jgit.dircache.*;
- import org.eclipse.jgit.lib.Repository;
- import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-
- String outputTimestamp = project.getProperties().getProperty("project.build.outputTimestamp");
- long timestampMillis = java.time.Instant.parse(outputTimestamp).toEpochMilli();
- zip(String zipFileName, Map pathByFile) {
- OutputStream outputStream = new FileOutputStream(zipFileName);
- ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
- try {
- for (String file : pathByFile.keySet()) {
- Path path = pathByFile.get(file);
- // Skip directories, which is tracked by Git only when it is a symlink
- if (Files.isDirectory(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- continue;
- }
- try {
- ZipEntry zipEntry = new ZipEntry(file);
- zipEntry.setTime(timestampMillis);
- zipOutputStream.putNextEntry(zipEntry);
- zipOutputStream.write(Files.readAllBytes(path));
- zipOutputStream.closeEntry();
- } catch (Exception error) {
- String message = String.format(
- "failed processing file `%s` at path `%s`",
- new Object[]{file, path});
- throw new RuntimeException(message, error);
- }
- }
- } catch (Exception error) {
- String message = String.format("failed creating ZIP archive `%s`", new Object[]{zipFileName});
- Exception extendedError = new RuntimeException(message, error);
- // Supplement diagnostics
- extendedError.printStackTrace(System.err);
- throw extendedError;
- } finally {
- zipOutputStream.close();
- }
- }
-
- // Find Git-tracked files
- SortedMap pathByFile = new TreeMap();
- Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build();
- DirCache repoCache = repo.readDirCache();
- String repoDirectoryParent = repo.getDirectory().getParent();
- for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
- DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex);
- String repoCacheEntryPath = repoCacheEntry.getPathString();
- pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath());
- }
-
- // Create the source distribution using Git-tracked files
- zip("target/src.zip", pathByFile);
- System.out.format("Generated the source distribution (`src.zip`) containing %d files.%n", new Object[]{pathByFile.size()});
-
- // Short-circuit if there is no binary distribution expected
- if (${attachmentCount} == 0) {
- return;
- }
-
- // Find auxiliary files that will go into the binary distribution
- SortedMap pathByFile = new TreeMap();
- pathByFile.put("RELEASE-NOTES.adoc", new File("${project.build.directory}/generated-site/antora/modules/ROOT/pages/_release-notes/${project.version}.adoc").toPath());
- pathByFile.put("README.adoc", new File("README.adoc").toPath());
- pathByFile.put("NOTICE.txt", new File("NOTICE.txt").toPath());
- pathByFile.put("LICENSE.txt", new File("LICENSE.txt").toPath());
-
- // Find attachments that will go into the binary distribution
- Path projectDir = Paths.get("${maven.multiModuleProjectDirectory}", new String[0]);
- String attachmentFilepathPattern = "${attachmentFilepathPattern}";
- System.out.format("Locating attachments matching the provided pattern: `%s`%n", new Object[] {attachmentFilepathPattern});
- SortedMap attachmentPathByFile = new TreeMap();
- Stream paths = Files.walk(new File(repoDirectoryParent).toPath(), 32, /* required for BSH method resolution: */ new FileVisitOption[0]);
- try {
- paths.forEach(new Consumer() {
- public void accept(Path path) {
- if (Files.isRegularFile(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- String relativePath = projectDir.relativize(path).toString();
- if (relativePath.matches(attachmentFilepathPattern)) {
- attachmentPathByFile.put(path.getFileName().toString(), path);
- }
- }
- }
- });
- } catch (Exception error) {
- // Supplement diagnostics
- error.printStackTrace(System.err);
- throw error;
- } finally {
- paths.close();
- }
-
- // Check if no attachments were found
- if (${attachmentCount} != attachmentPathByFile.size()) {
- System.err.println("Attachments:");
- int[] i = {0};
- attachmentPathByFile.values().forEach(new Consumer() {
- public void accept(Path path) {
- System.err.format(" [%2d] %s%n", new Object[]{++i[0], path});
- }
- });
- System.err.format(
- "Error: Was expecting %d attachments, found %d!%n",
- new Object[]{${attachmentCount}, attachmentPathByFile.size()});
- System.err.println("Tip: Have you already executed the Maven `package` goal?");
- System.exit(1);
- }
-
- // Create the binary distribution
- pathByFile.putAll(attachmentPathByFile);
- zip("target/bin.zip", pathByFile);
- System.out.format("Generated the binary distribution (`bin.zip`) containing following %d files:%n", new Object[] {pathByFile.size()});
- for (String file : pathByFile.keySet()) {
- System.out.println("-> " + file);
- }]]></script>
+ <scripts>
+ <script><![CDATA[import java.io.*;
+ import java.nio.file.*;
+ import java.util.*;
+ import java.util.function.*;
+ import java.util.regex.*;
+ import java.util.stream.*;
+ import java.util.zip.*;
+ import java.time.Instant;
+
+ import org.eclipse.jgit.dircache.*;
+ import org.eclipse.jgit.lib.Repository;
+ import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+ long timestampMillis = Instant.parse(project.properties['project.build.outputTimestamp']).toEpochMilli()
+ def getProperty = { String name ->
+ return session.userProperties[name] ?: session.systemProperties[name]
+ }
+ // These are ensured by the `enforcer` rule
+ int attachmentCount = getProperty('attachmentCount').toInteger()
+ Pattern attachmentFilepathPattern = Pattern.compile(getProperty('attachmentFilepathPattern'))
+
+ def zip = { String zipFileName, Map<String, Path> pathByFile ->
+ try (OutputStream outputStream = new FileOutputStream(zipFileName)
+ ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
+ pathByFile.each {
+ // Skip directories, which is tracked by Git only when it is a symlink
+ if (!Files.isDirectory(it.value)) {
+ try {
+ log.debug('adding file `' + it.key + '` at path `' + it.value + '`')
+ ZipEntry zipEntry = new ZipEntry(it.key)
+ zipEntry.setTime(timestampMillis)
+ zipOutputStream.putNextEntry(zipEntry)
+ zipOutputStream.write(Files.readAllBytes(it.value))
+ zipOutputStream.closeEntry()
+ } catch (Exception error) {
+ log.error('failed processing file `' + it.key + '` at path `' + it.value + '`', error)
+ throw error
+ }
+ }
+
+ }
+ } catch (Exception error) {
+ log.error('failed creating ZIP archive `' + zipFileName + '`', error)
+ throw error
+ }
+ }
+
+ // Find Git-tracked files
+ SortedMap pathByFile = new TreeMap()
+ Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build()
+ DirCache repoCache = repo.readDirCache()
+ String repoDirectoryParent = repo.getDirectory().getParent()
+ for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
+ DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex)
+ String repoCacheEntryPath = repoCacheEntry.getPathString()
+ pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath())
+ }
+
+ // Create the source distribution using Git-tracked files
+ zip(project.build.directory + '/src.zip', pathByFile)
+ log.info('Generated the source distribution (`src.zip`) containing ' + pathByFile.size() + ' files.')
+
+ // Short-circuit if there is no binary distribution expected
+ if (attachmentCount == 0) {
+ return
+ }
+
+ // Find auxiliary files that will go into the binary distribution
+ pathByFile = new TreeMap()
+ String releaseNotes = project.build.directory +
+ '/generated-site/antora/modules/ROOT/pages/_release-notes/' +
+ project.version + '.adoc'
+ pathByFile.put("RELEASE-NOTES.adoc", Paths.get(releaseNotes))
+ pathByFile.put("README.adoc", Paths.get("README.adoc"))
+ pathByFile.put("NOTICE.txt", Paths.get("NOTICE.txt"))
+ pathByFile.put("LICENSE.txt", Paths.get("LICENSE.txt"))
+
+ // Find attachments that will go into the binary distribution
+ Path projectDir = Paths.get(getProperty('maven.multiModuleProjectDirectory'))
+ log.info("Locating attachments matching the provided pattern: ${d}{attachmentFilepathPattern}")
Review Comment:
Fixed in https://github.com/apache/logging-parent/pull/241/commits/40eb92c6c85f5b419280510ea27bb4c34cbc376c.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace BeanShell with Groovy [logging-parent]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #241:
URL: https://github.com/apache/logging-parent/pull/241#discussion_r1763193498
##########
pom.xml:
##########
@@ -364,6 +360,26 @@
<version>${flatten-maven-plugin.version}</version>
</plugin>
+ <plugin>
+ <groupId>org.codehaus.gmavenplus</groupId>
+ <artifactId>gmavenplus-plugin</artifactId>
+ <version>${gmavenplus-plugin.version}</version>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.groovy</groupId>
+ <artifactId>groovy</artifactId>
+ <version>${groovy.version}</version>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.groovy</groupId>
+ <artifactId>groovy-ant</artifactId>
Review Comment:
To remove a warning of GMavenPlus.
If I remove it everything works, but there is a warning.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace BeanShell with Groovy [logging-parent]
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #241:
URL: https://github.com/apache/logging-parent/pull/241#discussion_r1763192401
##########
pom.xml:
##########
@@ -1154,131 +1170,128 @@
<execution>
<id>create-distribution</id>
<goals>
- <goal>run</goal>
+ <goal>execute</goal>
</goals>
<configuration>
- <script><![CDATA[import java.io.*;
- import java.nio.file.*;
- import java.util.*;
- import java.util.function.*;
- import java.util.stream.*;
- import java.util.zip.*;
-
- import org.eclipse.jgit.dircache.*;
- import org.eclipse.jgit.lib.Repository;
- import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-
- String outputTimestamp = project.getProperties().getProperty("project.build.outputTimestamp");
- long timestampMillis = java.time.Instant.parse(outputTimestamp).toEpochMilli();
- zip(String zipFileName, Map pathByFile) {
- OutputStream outputStream = new FileOutputStream(zipFileName);
- ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
- try {
- for (String file : pathByFile.keySet()) {
- Path path = pathByFile.get(file);
- // Skip directories, which is tracked by Git only when it is a symlink
- if (Files.isDirectory(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- continue;
- }
- try {
- ZipEntry zipEntry = new ZipEntry(file);
- zipEntry.setTime(timestampMillis);
- zipOutputStream.putNextEntry(zipEntry);
- zipOutputStream.write(Files.readAllBytes(path));
- zipOutputStream.closeEntry();
- } catch (Exception error) {
- String message = String.format(
- "failed processing file `%s` at path `%s`",
- new Object[]{file, path});
- throw new RuntimeException(message, error);
- }
- }
- } catch (Exception error) {
- String message = String.format("failed creating ZIP archive `%s`", new Object[]{zipFileName});
- Exception extendedError = new RuntimeException(message, error);
- // Supplement diagnostics
- extendedError.printStackTrace(System.err);
- throw extendedError;
- } finally {
- zipOutputStream.close();
- }
- }
-
- // Find Git-tracked files
- SortedMap pathByFile = new TreeMap();
- Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build();
- DirCache repoCache = repo.readDirCache();
- String repoDirectoryParent = repo.getDirectory().getParent();
- for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
- DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex);
- String repoCacheEntryPath = repoCacheEntry.getPathString();
- pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath());
- }
-
- // Create the source distribution using Git-tracked files
- zip("target/src.zip", pathByFile);
- System.out.format("Generated the source distribution (`src.zip`) containing %d files.%n", new Object[]{pathByFile.size()});
-
- // Short-circuit if there is no binary distribution expected
- if (${attachmentCount} == 0) {
- return;
- }
-
- // Find auxiliary files that will go into the binary distribution
- SortedMap pathByFile = new TreeMap();
- pathByFile.put("RELEASE-NOTES.adoc", new File("${project.build.directory}/generated-site/antora/modules/ROOT/pages/_release-notes/${project.version}.adoc").toPath());
- pathByFile.put("README.adoc", new File("README.adoc").toPath());
- pathByFile.put("NOTICE.txt", new File("NOTICE.txt").toPath());
- pathByFile.put("LICENSE.txt", new File("LICENSE.txt").toPath());
-
- // Find attachments that will go into the binary distribution
- Path projectDir = Paths.get("${maven.multiModuleProjectDirectory}", new String[0]);
- String attachmentFilepathPattern = "${attachmentFilepathPattern}";
- System.out.format("Locating attachments matching the provided pattern: `%s`%n", new Object[] {attachmentFilepathPattern});
- SortedMap attachmentPathByFile = new TreeMap();
- Stream paths = Files.walk(new File(repoDirectoryParent).toPath(), 32, /* required for BSH method resolution: */ new FileVisitOption[0]);
- try {
- paths.forEach(new Consumer() {
- public void accept(Path path) {
- if (Files.isRegularFile(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- String relativePath = projectDir.relativize(path).toString();
- if (relativePath.matches(attachmentFilepathPattern)) {
- attachmentPathByFile.put(path.getFileName().toString(), path);
- }
- }
- }
- });
- } catch (Exception error) {
- // Supplement diagnostics
- error.printStackTrace(System.err);
- throw error;
- } finally {
- paths.close();
- }
-
- // Check if no attachments were found
- if (${attachmentCount} != attachmentPathByFile.size()) {
- System.err.println("Attachments:");
- int[] i = {0};
- attachmentPathByFile.values().forEach(new Consumer() {
- public void accept(Path path) {
- System.err.format(" [%2d] %s%n", new Object[]{++i[0], path});
- }
- });
- System.err.format(
- "Error: Was expecting %d attachments, found %d!%n",
- new Object[]{${attachmentCount}, attachmentPathByFile.size()});
- System.err.println("Tip: Have you already executed the Maven `package` goal?");
- System.exit(1);
- }
-
- // Create the binary distribution
- pathByFile.putAll(attachmentPathByFile);
- zip("target/bin.zip", pathByFile);
- System.out.format("Generated the binary distribution (`bin.zip`) containing following %d files:%n", new Object[] {pathByFile.size()});
- for (String file : pathByFile.keySet()) {
- System.out.println("-> " + file);
- }]]></script>
+ <scripts>
+ <script><![CDATA[import java.io.*;
+ import java.nio.file.*;
+ import java.util.*;
+ import java.util.function.*;
+ import java.util.regex.*;
+ import java.util.stream.*;
+ import java.util.zip.*;
+ import java.time.Instant;
+
+ import org.eclipse.jgit.dircache.*;
+ import org.eclipse.jgit.lib.Repository;
+ import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+ long timestampMillis = Instant.parse(project.properties['project.build.outputTimestamp']).toEpochMilli()
+ def getProperty = { String name ->
+ return session.userProperties[name] ?: session.systemProperties[name]
+ }
+ // These are ensured by the `enforcer` rule
+ int attachmentCount = getProperty('attachmentCount').toInteger()
+ Pattern attachmentFilepathPattern = Pattern.compile(getProperty('attachmentFilepathPattern'))
+
+ def zip = { String zipFileName, Map<String, Path> pathByFile ->
+ try (OutputStream outputStream = new FileOutputStream(zipFileName)
+ ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
+ pathByFile.each {
+ // Skip directories, which is tracked by Git only when it is a symlink
+ if (!Files.isDirectory(it.value)) {
+ try {
+ log.debug('adding file `' + it.key + '` at path `' + it.value + '`')
+ ZipEntry zipEntry = new ZipEntry(it.key)
+ zipEntry.setTime(timestampMillis)
+ zipOutputStream.putNextEntry(zipEntry)
+ zipOutputStream.write(Files.readAllBytes(it.value))
+ zipOutputStream.closeEntry()
+ } catch (Exception error) {
+ log.error('failed processing file `' + it.key + '` at path `' + it.value + '`', error)
+ throw error
+ }
+ }
+
+ }
+ } catch (Exception error) {
+ log.error('failed creating ZIP archive `' + zipFileName + '`', error)
+ throw error
+ }
+ }
+
+ // Find Git-tracked files
+ SortedMap pathByFile = new TreeMap()
+ Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build()
+ DirCache repoCache = repo.readDirCache()
+ String repoDirectoryParent = repo.getDirectory().getParent()
+ for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
+ DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex)
+ String repoCacheEntryPath = repoCacheEntry.getPathString()
+ pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath())
+ }
+
+ // Create the source distribution using Git-tracked files
+ zip(project.build.directory + '/src.zip', pathByFile)
+ log.info('Generated the source distribution (`src.zip`) containing ' + pathByFile.size() + ' files.')
+
+ // Short-circuit if there is no binary distribution expected
+ if (attachmentCount == 0) {
+ return
+ }
+
+ // Find auxiliary files that will go into the binary distribution
+ pathByFile = new TreeMap()
+ String releaseNotes = project.build.directory +
+ '/generated-site/antora/modules/ROOT/pages/_release-notes/' +
+ project.version + '.adoc'
+ pathByFile.put("RELEASE-NOTES.adoc", Paths.get(releaseNotes))
+ pathByFile.put("README.adoc", Paths.get("README.adoc"))
+ pathByFile.put("NOTICE.txt", Paths.get("NOTICE.txt"))
+ pathByFile.put("LICENSE.txt", Paths.get("LICENSE.txt"))
+
+ // Find attachments that will go into the binary distribution
+ Path projectDir = Paths.get(getProperty('maven.multiModuleProjectDirectory'))
+ log.info("Locating attachments matching the provided pattern: ${d}{attachmentFilepathPattern}")
+ SortedMap attachmentPathByFile = new TreeMap()
+ Stream paths = Files.walk(Paths.get(repoDirectoryParent), 32)
+ try {
+ paths.forEach { path ->
+ if (Files.isRegularFile(path)) {
+ String relativePath = projectDir.relativize(path).toString()
+ if (attachmentFilepathPattern.matcher(relativePath).matches()) {
+ attachmentPathByFile.put(path.getFileName().toString(), path)
+ }
+ }
+ }
+ } catch (Exception error) {
+ // Supplement diagnostics
+ log.error(error)
+ throw error
+ } finally {
+ paths.close()
+ }
+
+ StringBuilder attachments = new StringBuilder()
+ attachmentPathByFile.eachWithIndex { entry, index ->
+ attachments.append('\n\t[' + (index + 1) + '] ' + entry.value);
+ }
+ // Check if no attachments were found
+ if (attachmentCount != attachmentPathByFile.size()) {
+ log.error('Attachments found:' + attachments)
+ String errorMsg = 'Error: Was expecting ' + attachmentCount + ' attachments, found ' + attachmentPathByFile.size() + '!'
+ log.error(errorMsg)
Review Comment:
This is not a Log4j API logger, but a [`o.a.maven.plugin.logging.Log`](https://maven.apache.org/ref/3.9.8/maven-plugin-api/apidocs/org/apache/maven/plugin/logging/Log.html). Sorry :wink:!
If you are referring to Groovy's string interpolation, I didn't manage to get it working: even if I use something like `${d}{lala}`, Maven will expand it twice into `${lala}` (assuming `${d}` expands to `$`) and then into the value of the `lala` property.
I confirmed it with a debugger.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace BeanShell with Groovy [logging-parent]
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on code in PR #241:
URL: https://github.com/apache/logging-parent/pull/241#discussion_r1763080088
##########
pom.xml:
##########
@@ -364,6 +360,26 @@
<version>${flatten-maven-plugin.version}</version>
</plugin>
+ <plugin>
+ <groupId>org.codehaus.gmavenplus</groupId>
+ <artifactId>gmavenplus-plugin</artifactId>
+ <version>${gmavenplus-plugin.version}</version>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.groovy</groupId>
+ <artifactId>groovy</artifactId>
+ <version>${groovy.version}</version>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.groovy</groupId>
+ <artifactId>groovy-ant</artifactId>
Review Comment:
Why do we need this artifact?
##########
pom.xml:
##########
@@ -1154,131 +1170,128 @@
<execution>
<id>create-distribution</id>
<goals>
- <goal>run</goal>
+ <goal>execute</goal>
</goals>
<configuration>
- <script><![CDATA[import java.io.*;
- import java.nio.file.*;
- import java.util.*;
- import java.util.function.*;
- import java.util.stream.*;
- import java.util.zip.*;
-
- import org.eclipse.jgit.dircache.*;
- import org.eclipse.jgit.lib.Repository;
- import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-
- String outputTimestamp = project.getProperties().getProperty("project.build.outputTimestamp");
- long timestampMillis = java.time.Instant.parse(outputTimestamp).toEpochMilli();
- zip(String zipFileName, Map pathByFile) {
- OutputStream outputStream = new FileOutputStream(zipFileName);
- ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
- try {
- for (String file : pathByFile.keySet()) {
- Path path = pathByFile.get(file);
- // Skip directories, which is tracked by Git only when it is a symlink
- if (Files.isDirectory(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- continue;
- }
- try {
- ZipEntry zipEntry = new ZipEntry(file);
- zipEntry.setTime(timestampMillis);
- zipOutputStream.putNextEntry(zipEntry);
- zipOutputStream.write(Files.readAllBytes(path));
- zipOutputStream.closeEntry();
- } catch (Exception error) {
- String message = String.format(
- "failed processing file `%s` at path `%s`",
- new Object[]{file, path});
- throw new RuntimeException(message, error);
- }
- }
- } catch (Exception error) {
- String message = String.format("failed creating ZIP archive `%s`", new Object[]{zipFileName});
- Exception extendedError = new RuntimeException(message, error);
- // Supplement diagnostics
- extendedError.printStackTrace(System.err);
- throw extendedError;
- } finally {
- zipOutputStream.close();
- }
- }
-
- // Find Git-tracked files
- SortedMap pathByFile = new TreeMap();
- Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build();
- DirCache repoCache = repo.readDirCache();
- String repoDirectoryParent = repo.getDirectory().getParent();
- for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
- DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex);
- String repoCacheEntryPath = repoCacheEntry.getPathString();
- pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath());
- }
-
- // Create the source distribution using Git-tracked files
- zip("target/src.zip", pathByFile);
- System.out.format("Generated the source distribution (`src.zip`) containing %d files.%n", new Object[]{pathByFile.size()});
-
- // Short-circuit if there is no binary distribution expected
- if (${attachmentCount} == 0) {
- return;
- }
-
- // Find auxiliary files that will go into the binary distribution
- SortedMap pathByFile = new TreeMap();
- pathByFile.put("RELEASE-NOTES.adoc", new File("${project.build.directory}/generated-site/antora/modules/ROOT/pages/_release-notes/${project.version}.adoc").toPath());
- pathByFile.put("README.adoc", new File("README.adoc").toPath());
- pathByFile.put("NOTICE.txt", new File("NOTICE.txt").toPath());
- pathByFile.put("LICENSE.txt", new File("LICENSE.txt").toPath());
-
- // Find attachments that will go into the binary distribution
- Path projectDir = Paths.get("${maven.multiModuleProjectDirectory}", new String[0]);
- String attachmentFilepathPattern = "${attachmentFilepathPattern}";
- System.out.format("Locating attachments matching the provided pattern: `%s`%n", new Object[] {attachmentFilepathPattern});
- SortedMap attachmentPathByFile = new TreeMap();
- Stream paths = Files.walk(new File(repoDirectoryParent).toPath(), 32, /* required for BSH method resolution: */ new FileVisitOption[0]);
- try {
- paths.forEach(new Consumer() {
- public void accept(Path path) {
- if (Files.isRegularFile(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- String relativePath = projectDir.relativize(path).toString();
- if (relativePath.matches(attachmentFilepathPattern)) {
- attachmentPathByFile.put(path.getFileName().toString(), path);
- }
- }
- }
- });
- } catch (Exception error) {
- // Supplement diagnostics
- error.printStackTrace(System.err);
- throw error;
- } finally {
- paths.close();
- }
-
- // Check if no attachments were found
- if (${attachmentCount} != attachmentPathByFile.size()) {
- System.err.println("Attachments:");
- int[] i = {0};
- attachmentPathByFile.values().forEach(new Consumer() {
- public void accept(Path path) {
- System.err.format(" [%2d] %s%n", new Object[]{++i[0], path});
- }
- });
- System.err.format(
- "Error: Was expecting %d attachments, found %d!%n",
- new Object[]{${attachmentCount}, attachmentPathByFile.size()});
- System.err.println("Tip: Have you already executed the Maven `package` goal?");
- System.exit(1);
- }
-
- // Create the binary distribution
- pathByFile.putAll(attachmentPathByFile);
- zip("target/bin.zip", pathByFile);
- System.out.format("Generated the binary distribution (`bin.zip`) containing following %d files:%n", new Object[] {pathByFile.size()});
- for (String file : pathByFile.keySet()) {
- System.out.println("-> " + file);
- }]]></script>
+ <scripts>
+ <script><![CDATA[import java.io.*;
+ import java.nio.file.*;
+ import java.util.*;
+ import java.util.function.*;
+ import java.util.regex.*;
+ import java.util.stream.*;
+ import java.util.zip.*;
+ import java.time.Instant;
+
+ import org.eclipse.jgit.dircache.*;
+ import org.eclipse.jgit.lib.Repository;
+ import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+ long timestampMillis = Instant.parse(project.properties['project.build.outputTimestamp']).toEpochMilli()
+ def getProperty = { String name ->
+ return session.userProperties[name] ?: session.systemProperties[name]
+ }
+ // These are ensured by the `enforcer` rule
+ int attachmentCount = getProperty('attachmentCount').toInteger()
+ Pattern attachmentFilepathPattern = Pattern.compile(getProperty('attachmentFilepathPattern'))
+
+ def zip = { String zipFileName, Map<String, Path> pathByFile ->
+ try (OutputStream outputStream = new FileOutputStream(zipFileName)
+ ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
+ pathByFile.each {
+ // Skip directories, which is tracked by Git only when it is a symlink
+ if (!Files.isDirectory(it.value)) {
+ try {
+ log.debug('adding file `' + it.key + '` at path `' + it.value + '`')
+ ZipEntry zipEntry = new ZipEntry(it.key)
+ zipEntry.setTime(timestampMillis)
+ zipOutputStream.putNextEntry(zipEntry)
+ zipOutputStream.write(Files.readAllBytes(it.value))
+ zipOutputStream.closeEntry()
+ } catch (Exception error) {
+ log.error('failed processing file `' + it.key + '` at path `' + it.value + '`', error)
+ throw error
+ }
+ }
+
+ }
+ } catch (Exception error) {
+ log.error('failed creating ZIP archive `' + zipFileName + '`', error)
+ throw error
+ }
+ }
+
+ // Find Git-tracked files
+ SortedMap pathByFile = new TreeMap()
+ Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build()
+ DirCache repoCache = repo.readDirCache()
+ String repoDirectoryParent = repo.getDirectory().getParent()
+ for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
+ DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex)
+ String repoCacheEntryPath = repoCacheEntry.getPathString()
+ pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath())
+ }
+
+ // Create the source distribution using Git-tracked files
+ zip(project.build.directory + '/src.zip', pathByFile)
+ log.info('Generated the source distribution (`src.zip`) containing ' + pathByFile.size() + ' files.')
+
+ // Short-circuit if there is no binary distribution expected
+ if (attachmentCount == 0) {
+ return
+ }
+
+ // Find auxiliary files that will go into the binary distribution
+ pathByFile = new TreeMap()
+ String releaseNotes = project.build.directory +
+ '/generated-site/antora/modules/ROOT/pages/_release-notes/' +
+ project.version + '.adoc'
+ pathByFile.put("RELEASE-NOTES.adoc", Paths.get(releaseNotes))
+ pathByFile.put("README.adoc", Paths.get("README.adoc"))
+ pathByFile.put("NOTICE.txt", Paths.get("NOTICE.txt"))
+ pathByFile.put("LICENSE.txt", Paths.get("LICENSE.txt"))
+
+ // Find attachments that will go into the binary distribution
+ Path projectDir = Paths.get(getProperty('maven.multiModuleProjectDirectory'))
+ log.info("Locating attachments matching the provided pattern: ${d}{attachmentFilepathPattern}")
Review Comment:
Can we stick to one quoting style throughout all Groovy scripts, please? (I'd prefer double-quotes, to match the Java style followed in the rest of the code base.)
##########
pom.xml:
##########
@@ -1154,131 +1170,128 @@
<execution>
<id>create-distribution</id>
<goals>
- <goal>run</goal>
+ <goal>execute</goal>
</goals>
<configuration>
- <script><![CDATA[import java.io.*;
- import java.nio.file.*;
- import java.util.*;
- import java.util.function.*;
- import java.util.stream.*;
- import java.util.zip.*;
-
- import org.eclipse.jgit.dircache.*;
- import org.eclipse.jgit.lib.Repository;
- import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-
- String outputTimestamp = project.getProperties().getProperty("project.build.outputTimestamp");
- long timestampMillis = java.time.Instant.parse(outputTimestamp).toEpochMilli();
- zip(String zipFileName, Map pathByFile) {
- OutputStream outputStream = new FileOutputStream(zipFileName);
- ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
- try {
- for (String file : pathByFile.keySet()) {
- Path path = pathByFile.get(file);
- // Skip directories, which is tracked by Git only when it is a symlink
- if (Files.isDirectory(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- continue;
- }
- try {
- ZipEntry zipEntry = new ZipEntry(file);
- zipEntry.setTime(timestampMillis);
- zipOutputStream.putNextEntry(zipEntry);
- zipOutputStream.write(Files.readAllBytes(path));
- zipOutputStream.closeEntry();
- } catch (Exception error) {
- String message = String.format(
- "failed processing file `%s` at path `%s`",
- new Object[]{file, path});
- throw new RuntimeException(message, error);
- }
- }
- } catch (Exception error) {
- String message = String.format("failed creating ZIP archive `%s`", new Object[]{zipFileName});
- Exception extendedError = new RuntimeException(message, error);
- // Supplement diagnostics
- extendedError.printStackTrace(System.err);
- throw extendedError;
- } finally {
- zipOutputStream.close();
- }
- }
-
- // Find Git-tracked files
- SortedMap pathByFile = new TreeMap();
- Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build();
- DirCache repoCache = repo.readDirCache();
- String repoDirectoryParent = repo.getDirectory().getParent();
- for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
- DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex);
- String repoCacheEntryPath = repoCacheEntry.getPathString();
- pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath());
- }
-
- // Create the source distribution using Git-tracked files
- zip("target/src.zip", pathByFile);
- System.out.format("Generated the source distribution (`src.zip`) containing %d files.%n", new Object[]{pathByFile.size()});
-
- // Short-circuit if there is no binary distribution expected
- if (${attachmentCount} == 0) {
- return;
- }
-
- // Find auxiliary files that will go into the binary distribution
- SortedMap pathByFile = new TreeMap();
- pathByFile.put("RELEASE-NOTES.adoc", new File("${project.build.directory}/generated-site/antora/modules/ROOT/pages/_release-notes/${project.version}.adoc").toPath());
- pathByFile.put("README.adoc", new File("README.adoc").toPath());
- pathByFile.put("NOTICE.txt", new File("NOTICE.txt").toPath());
- pathByFile.put("LICENSE.txt", new File("LICENSE.txt").toPath());
-
- // Find attachments that will go into the binary distribution
- Path projectDir = Paths.get("${maven.multiModuleProjectDirectory}", new String[0]);
- String attachmentFilepathPattern = "${attachmentFilepathPattern}";
- System.out.format("Locating attachments matching the provided pattern: `%s`%n", new Object[] {attachmentFilepathPattern});
- SortedMap attachmentPathByFile = new TreeMap();
- Stream paths = Files.walk(new File(repoDirectoryParent).toPath(), 32, /* required for BSH method resolution: */ new FileVisitOption[0]);
- try {
- paths.forEach(new Consumer() {
- public void accept(Path path) {
- if (Files.isRegularFile(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- String relativePath = projectDir.relativize(path).toString();
- if (relativePath.matches(attachmentFilepathPattern)) {
- attachmentPathByFile.put(path.getFileName().toString(), path);
- }
- }
- }
- });
- } catch (Exception error) {
- // Supplement diagnostics
- error.printStackTrace(System.err);
- throw error;
- } finally {
- paths.close();
- }
-
- // Check if no attachments were found
- if (${attachmentCount} != attachmentPathByFile.size()) {
- System.err.println("Attachments:");
- int[] i = {0};
- attachmentPathByFile.values().forEach(new Consumer() {
- public void accept(Path path) {
- System.err.format(" [%2d] %s%n", new Object[]{++i[0], path});
- }
- });
- System.err.format(
- "Error: Was expecting %d attachments, found %d!%n",
- new Object[]{${attachmentCount}, attachmentPathByFile.size()});
- System.err.println("Tip: Have you already executed the Maven `package` goal?");
- System.exit(1);
- }
-
- // Create the binary distribution
- pathByFile.putAll(attachmentPathByFile);
- zip("target/bin.zip", pathByFile);
- System.out.format("Generated the binary distribution (`bin.zip`) containing following %d files:%n", new Object[] {pathByFile.size()});
- for (String file : pathByFile.keySet()) {
- System.out.println("-> " + file);
- }]]></script>
+ <scripts>
+ <script><![CDATA[import java.io.*;
+ import java.nio.file.*;
+ import java.util.*;
+ import java.util.function.*;
+ import java.util.regex.*;
+ import java.util.stream.*;
+ import java.util.zip.*;
+ import java.time.Instant;
+
+ import org.eclipse.jgit.dircache.*;
+ import org.eclipse.jgit.lib.Repository;
+ import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+ long timestampMillis = Instant.parse(project.properties['project.build.outputTimestamp']).toEpochMilli()
+ def getProperty = { String name ->
+ return session.userProperties[name] ?: session.systemProperties[name]
+ }
Review Comment:
Curious: What was wrong with hardcoding Maven properties?
##########
pom.xml:
##########
@@ -1154,131 +1170,128 @@
<execution>
<id>create-distribution</id>
<goals>
- <goal>run</goal>
+ <goal>execute</goal>
</goals>
<configuration>
- <script><![CDATA[import java.io.*;
- import java.nio.file.*;
- import java.util.*;
- import java.util.function.*;
- import java.util.stream.*;
- import java.util.zip.*;
-
- import org.eclipse.jgit.dircache.*;
- import org.eclipse.jgit.lib.Repository;
- import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-
- String outputTimestamp = project.getProperties().getProperty("project.build.outputTimestamp");
- long timestampMillis = java.time.Instant.parse(outputTimestamp).toEpochMilli();
- zip(String zipFileName, Map pathByFile) {
- OutputStream outputStream = new FileOutputStream(zipFileName);
- ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
- try {
- for (String file : pathByFile.keySet()) {
- Path path = pathByFile.get(file);
- // Skip directories, which is tracked by Git only when it is a symlink
- if (Files.isDirectory(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- continue;
- }
- try {
- ZipEntry zipEntry = new ZipEntry(file);
- zipEntry.setTime(timestampMillis);
- zipOutputStream.putNextEntry(zipEntry);
- zipOutputStream.write(Files.readAllBytes(path));
- zipOutputStream.closeEntry();
- } catch (Exception error) {
- String message = String.format(
- "failed processing file `%s` at path `%s`",
- new Object[]{file, path});
- throw new RuntimeException(message, error);
- }
- }
- } catch (Exception error) {
- String message = String.format("failed creating ZIP archive `%s`", new Object[]{zipFileName});
- Exception extendedError = new RuntimeException(message, error);
- // Supplement diagnostics
- extendedError.printStackTrace(System.err);
- throw extendedError;
- } finally {
- zipOutputStream.close();
- }
- }
-
- // Find Git-tracked files
- SortedMap pathByFile = new TreeMap();
- Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build();
- DirCache repoCache = repo.readDirCache();
- String repoDirectoryParent = repo.getDirectory().getParent();
- for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
- DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex);
- String repoCacheEntryPath = repoCacheEntry.getPathString();
- pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath());
- }
-
- // Create the source distribution using Git-tracked files
- zip("target/src.zip", pathByFile);
- System.out.format("Generated the source distribution (`src.zip`) containing %d files.%n", new Object[]{pathByFile.size()});
-
- // Short-circuit if there is no binary distribution expected
- if (${attachmentCount} == 0) {
- return;
- }
-
- // Find auxiliary files that will go into the binary distribution
- SortedMap pathByFile = new TreeMap();
- pathByFile.put("RELEASE-NOTES.adoc", new File("${project.build.directory}/generated-site/antora/modules/ROOT/pages/_release-notes/${project.version}.adoc").toPath());
- pathByFile.put("README.adoc", new File("README.adoc").toPath());
- pathByFile.put("NOTICE.txt", new File("NOTICE.txt").toPath());
- pathByFile.put("LICENSE.txt", new File("LICENSE.txt").toPath());
-
- // Find attachments that will go into the binary distribution
- Path projectDir = Paths.get("${maven.multiModuleProjectDirectory}", new String[0]);
- String attachmentFilepathPattern = "${attachmentFilepathPattern}";
- System.out.format("Locating attachments matching the provided pattern: `%s`%n", new Object[] {attachmentFilepathPattern});
- SortedMap attachmentPathByFile = new TreeMap();
- Stream paths = Files.walk(new File(repoDirectoryParent).toPath(), 32, /* required for BSH method resolution: */ new FileVisitOption[0]);
- try {
- paths.forEach(new Consumer() {
- public void accept(Path path) {
- if (Files.isRegularFile(path, /* required for BSH method resolution: */ new LinkOption[0])) {
- String relativePath = projectDir.relativize(path).toString();
- if (relativePath.matches(attachmentFilepathPattern)) {
- attachmentPathByFile.put(path.getFileName().toString(), path);
- }
- }
- }
- });
- } catch (Exception error) {
- // Supplement diagnostics
- error.printStackTrace(System.err);
- throw error;
- } finally {
- paths.close();
- }
-
- // Check if no attachments were found
- if (${attachmentCount} != attachmentPathByFile.size()) {
- System.err.println("Attachments:");
- int[] i = {0};
- attachmentPathByFile.values().forEach(new Consumer() {
- public void accept(Path path) {
- System.err.format(" [%2d] %s%n", new Object[]{++i[0], path});
- }
- });
- System.err.format(
- "Error: Was expecting %d attachments, found %d!%n",
- new Object[]{${attachmentCount}, attachmentPathByFile.size()});
- System.err.println("Tip: Have you already executed the Maven `package` goal?");
- System.exit(1);
- }
-
- // Create the binary distribution
- pathByFile.putAll(attachmentPathByFile);
- zip("target/bin.zip", pathByFile);
- System.out.format("Generated the binary distribution (`bin.zip`) containing following %d files:%n", new Object[] {pathByFile.size()});
- for (String file : pathByFile.keySet()) {
- System.out.println("-> " + file);
- }]]></script>
+ <scripts>
+ <script><![CDATA[import java.io.*;
+ import java.nio.file.*;
+ import java.util.*;
+ import java.util.function.*;
+ import java.util.regex.*;
+ import java.util.stream.*;
+ import java.util.zip.*;
+ import java.time.Instant;
+
+ import org.eclipse.jgit.dircache.*;
+ import org.eclipse.jgit.lib.Repository;
+ import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+ long timestampMillis = Instant.parse(project.properties['project.build.outputTimestamp']).toEpochMilli()
+ def getProperty = { String name ->
+ return session.userProperties[name] ?: session.systemProperties[name]
+ }
+ // These are ensured by the `enforcer` rule
+ int attachmentCount = getProperty('attachmentCount').toInteger()
+ Pattern attachmentFilepathPattern = Pattern.compile(getProperty('attachmentFilepathPattern'))
+
+ def zip = { String zipFileName, Map<String, Path> pathByFile ->
+ try (OutputStream outputStream = new FileOutputStream(zipFileName)
+ ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
+ pathByFile.each {
+ // Skip directories, which is tracked by Git only when it is a symlink
+ if (!Files.isDirectory(it.value)) {
+ try {
+ log.debug('adding file `' + it.key + '` at path `' + it.value + '`')
+ ZipEntry zipEntry = new ZipEntry(it.key)
+ zipEntry.setTime(timestampMillis)
+ zipOutputStream.putNextEntry(zipEntry)
+ zipOutputStream.write(Files.readAllBytes(it.value))
+ zipOutputStream.closeEntry()
+ } catch (Exception error) {
+ log.error('failed processing file `' + it.key + '` at path `' + it.value + '`', error)
+ throw error
+ }
+ }
+
+ }
+ } catch (Exception error) {
+ log.error('failed creating ZIP archive `' + zipFileName + '`', error)
+ throw error
+ }
+ }
+
+ // Find Git-tracked files
+ SortedMap pathByFile = new TreeMap()
+ Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir().build()
+ DirCache repoCache = repo.readDirCache()
+ String repoDirectoryParent = repo.getDirectory().getParent()
+ for (int repoCacheEntryIndex = 0; repoCacheEntryIndex < repoCache.getEntryCount(); repoCacheEntryIndex++) {
+ DirCacheEntry repoCacheEntry = repoCache.getEntry(repoCacheEntryIndex)
+ String repoCacheEntryPath = repoCacheEntry.getPathString()
+ pathByFile.put(repoCacheEntryPath, new File(repoDirectoryParent, repoCacheEntryPath).toPath())
+ }
+
+ // Create the source distribution using Git-tracked files
+ zip(project.build.directory + '/src.zip', pathByFile)
+ log.info('Generated the source distribution (`src.zip`) containing ' + pathByFile.size() + ' files.')
+
+ // Short-circuit if there is no binary distribution expected
+ if (attachmentCount == 0) {
+ return
+ }
+
+ // Find auxiliary files that will go into the binary distribution
+ pathByFile = new TreeMap()
+ String releaseNotes = project.build.directory +
+ '/generated-site/antora/modules/ROOT/pages/_release-notes/' +
+ project.version + '.adoc'
+ pathByFile.put("RELEASE-NOTES.adoc", Paths.get(releaseNotes))
+ pathByFile.put("README.adoc", Paths.get("README.adoc"))
+ pathByFile.put("NOTICE.txt", Paths.get("NOTICE.txt"))
+ pathByFile.put("LICENSE.txt", Paths.get("LICENSE.txt"))
+
+ // Find attachments that will go into the binary distribution
+ Path projectDir = Paths.get(getProperty('maven.multiModuleProjectDirectory'))
+ log.info("Locating attachments matching the provided pattern: ${d}{attachmentFilepathPattern}")
+ SortedMap attachmentPathByFile = new TreeMap()
+ Stream paths = Files.walk(Paths.get(repoDirectoryParent), 32)
+ try {
+ paths.forEach { path ->
+ if (Files.isRegularFile(path)) {
+ String relativePath = projectDir.relativize(path).toString()
+ if (attachmentFilepathPattern.matcher(relativePath).matches()) {
+ attachmentPathByFile.put(path.getFileName().toString(), path)
+ }
+ }
+ }
+ } catch (Exception error) {
+ // Supplement diagnostics
+ log.error(error)
+ throw error
+ } finally {
+ paths.close()
+ }
+
+ StringBuilder attachments = new StringBuilder()
+ attachmentPathByFile.eachWithIndex { entry, index ->
+ attachments.append('\n\t[' + (index + 1) + '] ' + entry.value);
+ }
+ // Check if no attachments were found
+ if (attachmentCount != attachmentPathByFile.size()) {
+ log.error('Attachments found:' + attachments)
+ String errorMsg = 'Error: Was expecting ' + attachmentCount + ' attachments, found ' + attachmentPathByFile.size() + '!'
+ log.error(errorMsg)
Review Comment:
Shall we use parameters in the message instead? (There are other `log` calls where string concatenation is preferred over parameters.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org