2

I have a Maven project where I want to have two build artifacts:

  1. The jar file containing the compiled Java source.
  2. A folder containing a number of .properties file.

How can I setup my Maven project to do this? And then, once I've done this, how can I consume them up the dependency graph?

Max
  • 15,157
  • 17
  • 82
  • 127
  • You could use two executions of the assembly plugin : one for source, another for the properties file. To consume them, you can attach them with the attach goal from the assembly plugin. But it's deprecated now... – davidxxx Jun 07 '15 at 09:53
  • @Gerold, I precised to use multiple executions of the assembly plugin. I don't know if it the proper approach but when your need is not standard, the approach often cannot be standard. I already done it and it works. – davidxxx Jun 07 '15 at 10:24
  • @davidh Looking at [Apache Maven Assembly Plugin, Introduction](https://maven.apache.org/plugins/maven-assembly-plugin/) I'm not sure whether this is the proper approach: _"intended to allow users to aggregate the project output [...] into a single **distributable archive**"._ The OP just wants them in a _folder._ – Gerold Broser Jun 07 '15 at 10:25
  • @davidh But you are somehow right, too. It works with the [assembly format `dir.`](https://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html#formats) The [`attached` goal is deprecated](https://maven.apache.org/plugins/maven-assembly-plugin/plugin-info.html) indeed. – Gerold Broser Jun 07 '15 at 10:35

1 Answers1

4

Add a copy-resources goal of the Maven Resources Plugin to your POM.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy-property-files</id>
            <phase>process-resources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/property-files</outputDirectory>
              <resources>          
                <resource>
                  ...
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

I can't understand what you mean exactly by "consume them up the dependency graph".

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • By "consume up the dependency graph" i mean that: I have a project (call it A) producing property files. Other projects depend on A, that is, they are up the dependency graph (or tree). In this other projects, I need a way of locating the properties artifact produced by A. – Max Jun 07 '15 at 11:10
  • Also, is it possible to set an entire directory as a resource? That would be nice so I need not explicitly list all the file. – Max Jun 07 '15 at 11:11
  • @Max See [Maven Resources Plugin, Including and excluding files and directories](https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html). – Gerold Broser Jun 07 '15 at 11:26
  • @Max Do you mean `B` referring to `A's` properties at runtime or do you mean adding `A's` property files to `B.jar` during `B.jar's` creation? – Gerold Broser Jun 07 '15 at 13:06
  • The former case (at runtime). To solve this issue I'm using the classloader list all URLs under the expected prefix. This should work right? – Max Jun 07 '15 at 13:30
  • 1
    @Max Yes, this should work. See also [How to use ClassLoader.getResources() correctly?](http://stackoverflow.com/questions/5193786/how-to-use-classloader-getresources-correctly). – Gerold Broser Jun 07 '15 at 13:39
  • @GeroldBroser, `copy-resources` created additional directory with all properties files in it; however, `mvn deploy` does not deploy them to configured remote repository. Can you suggest a way? – zendu Sep 25 '17 at 20:02
  • @zendu Please post an own question for this (mention that you looked at this question/answer here, and also all others you possibly looked at). Such your question will be seen by more people and most probably answered more quickly. Unfortunately I'm too busy to contribute to SO atm. – Gerold Broser Sep 26 '17 at 19:27