0

I have a Maven project that it's only a POM working as a BOM listing a set of common dependencies, which is then imported by other projects.

For illustration purposes, this is the BOM in question:

<?xml version="1.0" encoding="UTF-8"?>
<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>myorg</groupId>
    <artifactId>my-bom</artifactId>
    <version>1.2.3</version>
    <packaging>pom</packaging>
    <properties>
        <xyz.version>4.5.6</xyz.version>
        <abc.version>7.8.9</abc.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>foo.bar</groupId>
                <artifactId>xyz</artifactId>
                <version>${xyz.version}</version>
            </dependency>
            <dependency>
                <groupId>yolo</groupId>
                <artifactId>abc</artifactId>
                <version>${abc.version}</version>
            </dependency>
    </dependencies>
  </dependencyManagement>
</project>

For reasons specific to our organization and project, I would like to generate a properties file listing the properties (and their values) from this pom. Something like this:

abc.version=7.8.9
xyz.version=4.5.6

Generating the properties file can be done trivially as described in this answer here.

But it is not clear to me if I can attach this source with a 'properties' classifier and deploying it to our repo. At least not without adding a submodule of type jar or something.

Is there a way I can accomplish this, to attach an additional artifact (the generated 'properties' file) while keeping the BOM project as a single-module project?

luis.espinal
  • 10,331
  • 6
  • 39
  • 55
  • There is one thing. That your usage of parts in the dependency management is not like BOM should be used (https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#bill-of-materials-bom-poms) to do it you have to use `import` and `pom` furthermore you could use the [build-helper-maven-pulgin](https://www.mojohaus.org/build-helper-maven-plugin/usage.html) to attach a file (containing the properties) as a supplemental artifact (classifier:properties)... but I would not recommend that way because I don't understand the properties.. – khmarbaise Mar 27 '21 at 21:58

1 Answers1

0

You can use maven-assembly-plugin to build an archive (tar.gz or zip file) containing a property file.

pom.xml

<groupId>myorg</groupId>
<artifactId>my-bom</artifactId>
<version>1.2.3</version>
<packaging>pom</packaging>
<properties>
  <xyz.version>4.5.6</xyz.version>
  <abc.version>7.8.9</abc.version>
</properties>
<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <includes>
        <include>sample.properties</include>
      </includes>
    </resource>
  </resources>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.3.0</version>
      <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
          <descriptor>src/assembly/build.xml</descriptor>
        </descriptors>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
<dependencyManagement>
  <dependencies>
    ...
  </dependencies>
</dependencyManagement>

src/main/resources/sample.properties

xyz.version=${xyz.version}
abc.version=${abc.version}

src/assembly/build.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
    <id>copy-properties</id>
    <formats>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <outputDirectory/>
            <filtered>true</filtered>
            <includes>
                <include>sample.properties</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

So in addition to the pom file, you will get my-bom-1.2.3.tar.gz and my-com-1.2.3.tar.gz (containing a file with the filtered properties) in your repository.

Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56
  • This will, as you already wrote attach a tar.gz and zip file to the existing project but creates and compressed archives instead of the plain file as an artifact. – khmarbaise Mar 27 '21 at 21:59