This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-ear-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 03def92  Write the module paths in the EAR to an optional module map 
file (#579)
03def92 is described below

commit 03def92014511e2c81e705c99b53976c093f6987
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Oct 26 17:59:35 2026 +0100

    Write the module paths in the EAR to an optional module map file (#579)
---
 .../plugins/ear/GenerateApplicationXmlMojo.java    |  57 +++++++++++
 .../org/apache/maven/plugins/ear/it/EarMojoIT.java |  47 ++++++++++
 .../project-104/expected-META-INF/application.xml  |  41 ++++++++
 src/test/resources/projects/project-104/pom.xml    | 104 +++++++++++++++++++++
 4 files changed, 249 insertions(+)

diff --git 
a/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java 
b/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
index 4b59f59..48a08fe 100644
--- a/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
+++ b/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
@@ -18,12 +18,18 @@
  */
 package org.apache.maven.plugins.ear;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
+import java.util.Properties;
 
+import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -155,6 +161,18 @@ public class GenerateApplicationXmlMojo extends 
AbstractEarMojo {
     @Parameter
     private PlexusConfiguration resourceRefs;
 
+    /**
+     * A file to write the path of every module in the EAR to. It is a 
properties file whose keys are
+     * {@code groupId:artifactId:type} or {@code 
groupId:artifactId:type:classifier} and whose values are the paths of
+     * the modules in the EAR, for example {@code 
eartest:jar-sample-one:jar=libs/eartest-jar-sample-one-1.0.jar}.
+     * Tools that lay out the EAR themselves, for example as a loose 
application, can read it instead of reproducing
+     * how this plugin names and places modules. The file is not written 
unless this parameter is set.
+     *
+     * @since 3.5.0
+     */
+    @Parameter(property = "ear.moduleMapFile")
+    private File moduleMapFile;
+
     /**
      * {@inheritDoc}
      */
@@ -205,6 +223,45 @@ public class GenerateApplicationXmlMojo extends 
AbstractEarMojo {
                 throw new MojoExecutionException("Unable to copy jboss-app.xml 
to final destination", e);
             }
         }
+
+        if (moduleMapFile != null) {
+            writeModuleMap();
+        }
+    }
+
+    /**
+     * Writes {@link #moduleMapFile} with sorted lines and no timestamp, so 
that the file is reproducible.
+     *
+     * @throws MojoExecutionException if the file cannot be written
+     */
+    private void writeModuleMap() throws MojoExecutionException {
+        Properties map = new Properties();
+        for (EarModule module : getModules()) {
+            Artifact artifact = module.getArtifact();
+            String key = artifact.getGroupId() + ":" + 
artifact.getArtifactId() + ":" + artifact.getType();
+            if (artifact.getClassifier() != null && 
!artifact.getClassifier().isEmpty()) {
+                key += ":" + artifact.getClassifier();
+            }
+            map.setProperty(key, module.getUri());
+        }
+        try {
+            ByteArrayOutputStream stored = new ByteArrayOutputStream();
+            map.store(stored, null);
+            List<String> lines = new ArrayList<>();
+            for (String line : new String(stored.toByteArray(), 
StandardCharsets.ISO_8859_1).split("\\R")) {
+                if (!line.isEmpty() && !line.startsWith("#")) {
+                    lines.add(line);
+                }
+            }
+            Collections.sort(lines);
+            File parent = moduleMapFile.getAbsoluteFile().getParentFile();
+            if (parent != null) {
+                Files.createDirectories(parent.toPath());
+            }
+            Files.write(moduleMapFile.toPath(), lines, 
StandardCharsets.ISO_8859_1);
+        } catch (IOException e) {
+            throw new MojoExecutionException("Unable to write the module map 
to " + moduleMapFile, e);
+        }
     }
 
     /**
diff --git a/src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java 
b/src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java
index d36eb1a..51368f2 100644
--- a/src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java
+++ b/src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java
@@ -20,7 +20,16 @@ package org.apache.maven.plugins.ear.it;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 
@@ -1447,4 +1456,42 @@ class EarMojoIT extends AbstractEarPluginIT {
         assertFalse(badFile.exists(), "EAR filename should not contain spaces 
from whitespace classifier");
         assertTrue(expectedFile.exists(), "EAR archive not found at expected 
path without spaces");
     }
+
+    /**
+     * Builds an EAR with {@code moduleMapFile} set and checks that the map 
holds every module's path in the EAR,
+     * including a custom {@code bundleFileName}, a {@code bundleDir} and the 
{@code defaultLibBundleDir}.
+     */
+    @Test
+    void testProject104() throws Exception {
+        final File baseDir = doTestProject("project-104", new String[] {
+            "foo/eartest-ejb-sample-one-1.0.jar",
+            "eartest-ejb-sample-two-1.0.jar",
+            "com.foo.bar-ejb-sample-one-1.0.jar",
+            "com.foo.bar-ejb-sample-two-1.0.jar",
+            "libs/eartest-jar-sample-one-1.0.jar",
+            "libs/eartest-jar-sample-two-1.0.jar",
+            "sar-sample-one.sar"
+        });
+
+        final Path moduleMap = 
baseDir.toPath().resolve("target/ear-modules.properties");
+        final List<String> lines = Files.readAllLines(moduleMap, 
StandardCharsets.ISO_8859_1);
+        final List<String> sorted = new ArrayList<>(lines);
+        Collections.sort(sorted);
+        assertEquals(sorted, lines, "module map lines are sorted");
+        assertTrue(lines.stream().noneMatch(line -> line.startsWith("#")), 
"module map has no comment lines");
+
+        final Properties map = new Properties();
+        try (InputStream in = Files.newInputStream(moduleMap)) {
+            map.load(in);
+        }
+        final Map<String, String> expected = new HashMap<>();
+        expected.put("eartest:ejb-sample-one:ejb", 
"foo/eartest-ejb-sample-one-1.0.jar");
+        expected.put("eartest:ejb-sample-two:ejb", 
"eartest-ejb-sample-two-1.0.jar");
+        expected.put("com.foo.bar:ejb-sample-one:ejb", 
"com.foo.bar-ejb-sample-one-1.0.jar");
+        expected.put("com.foo.bar:ejb-sample-two:ejb", 
"com.foo.bar-ejb-sample-two-1.0.jar");
+        expected.put("eartest:jar-sample-one:jar", 
"libs/eartest-jar-sample-one-1.0.jar");
+        expected.put("eartest:jar-sample-two:jar", 
"libs/eartest-jar-sample-two-1.0.jar");
+        expected.put("eartest:sar-sample-one:sar", "sar-sample-one.sar");
+        assertEquals(expected, new HashMap<>(map));
+    }
 }
diff --git 
a/src/test/resources/projects/project-104/expected-META-INF/application.xml 
b/src/test/resources/projects/project-104/expected-META-INF/application.xml
new file mode 100644
index 0000000..2db43ad
--- /dev/null
+++ b/src/test/resources/projects/project-104/expected-META-INF/application.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<!DOCTYPE application PUBLIC
+       "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
+       "http://java.sun.com/dtd/application_1_3.dtd";>
+<application>
+  <display-name>maven-ear-plugin-test-project-104</display-name>
+  <module>
+    <connector>sar-sample-one.sar</connector>
+  </module>
+  <module>
+    <ejb>foo/eartest-ejb-sample-one-1.0.jar</ejb>
+  </module>
+  <module>
+    <ejb>eartest-ejb-sample-two-1.0.jar</ejb>
+  </module>
+  <module>
+    <ejb>com.foo.bar-ejb-sample-one-1.0.jar</ejb>
+  </module>
+  <module>
+    <ejb>com.foo.bar-ejb-sample-two-1.0.jar</ejb>
+  </module>
+</application>
\ No newline at end of file
diff --git a/src/test/resources/projects/project-104/pom.xml 
b/src/test/resources/projects/project-104/pom.xml
new file mode 100644
index 0000000..42149bd
--- /dev/null
+++ b/src/test/resources/projects/project-104/pom.xml
@@ -0,0 +1,104 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>ear</groupId>
+  <artifactId>maven-ear-plugin-test-project-104</artifactId>
+  <version>99.0</version>
+  <name>Maven</name>
+  <packaging>ear</packaging>
+  <dependencies>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>ejb-sample-one</artifactId>
+      <version>1.0</version>
+      <type>ejb</type>
+    </dependency>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>ejb-sample-two</artifactId>
+      <version>1.0</version>
+      <type>ejb</type>
+    </dependency>
+    <dependency>
+      <groupId>com.foo.bar</groupId>
+      <artifactId>ejb-sample-one</artifactId>
+      <version>1.0</version>
+      <type>ejb</type>
+    </dependency>
+    <dependency>
+      <groupId>com.foo.bar</groupId>
+      <artifactId>ejb-sample-two</artifactId>
+      <version>1.0</version>
+      <type>ejb</type>
+    </dependency>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>jar-sample-one</artifactId>
+      <version>1.0</version>
+      <type>jar</type>
+    </dependency>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>jar-sample-two</artifactId>
+      <version>1.0</version>
+      <type>jar</type>
+    </dependency>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>sar-sample-one</artifactId>
+      <version>1.0</version>
+      <type>sar</type>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-ear-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <!--
+            ! We are using by default outputFileNameMapping
+            ! with groupId-artifactId-version-classifier.extension 
+           -->
+<!--           <fileNameMapping>full</fileNameMapping> -->
+          <defaultLibBundleDir>libs</defaultLibBundleDir>
+          <moduleMapFile>target/ear-modules.properties</moduleMapFile>
+          <modules>
+            <sarModule>
+              <groupId>eartest</groupId>
+              <artifactId>sar-sample-one</artifactId>
+              <bundleFileName>sar-sample-one.sar</bundleFileName>
+            </sarModule>
+            <ejbModule>
+              <groupId>eartest</groupId>
+              <artifactId>ejb-sample-one</artifactId>
+              <bundleDir>foo</bundleDir>
+            </ejbModule>
+          </modules>
+          <version>1.3</version>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Reply via email to