How do I copy a file using Maven and specify the first module as the target folder?

95 Views Asked by At

There's more to it that the simple question in the subject. I've looked at the suggested links of "similar questions" and none of them completely helped.

BACKGROUND: Building a plugin system for an application. Part of that will require other teams to build the plugins themselves. The structure of each plugin that's built has a reactor project with two modules, one of which gets merged into the other using the shade plugin. This results in a single jar file that gets created in the target folder of one of the modules. That all works great. Because we have some things that all plugins are going to need (settings, structure, dependencies) we've built another project project-plugin-parent with only a POM that contains all of the common referenced and dependencies. Each plugin is then to reference this parent POM for all the common items, so that the devs don't need to worry about it. One of the parameters that's passed to the util is the jar file.

As part of that POM, we've got some utilities that we're marking as dependencies, which then get copied to a local utils folder. After that utility is downloaded, we then run it (depending on the profile that's used) ... In order to run it, it needs to be discoverable. When I put the dependency and the copy commands into the POM of the plugin (first module), it works great. But we want it to actually be in the project-plugin-parent POM... This puts the utils folder in the reactor's folder... OK, I can work with that. The problem happens when it then tries to run the utility... because it's in the parent POM, it tries to run it from there and so incorrectly passes the wrong jar file in the parameters.

OK, so here's what the structure looks like

project-plugin-parent.pom

plugin-project-reactor (with dependency on project-plugin-parent.pom
|
|-- plugin-project-main
|
\-- plugin-second 

I'd prefer if the util ended up inside of plugin-project-main... but if it ends up in the reactor structure that's fine. Somehow I need to be able to get to the folder/jar of the plugin-project-main. At the same time, I can't simply hard code the folder name as it will vary form project to project.

In the proect-plugin-parent, here's the configuration node for the maven copy command.

<configuration>
  <artifactItems>
      <artifactItem>
          <groupId>gov.va.bip.docgen</groupId>
          <artifactId>my-project-svc-rest</artifactId>
          <version>1.0.0</version>
          <type>jar</type>
          <destFileName>my-service.jar</destFileName>
      </artifactItem>
  </artifactItems>
  <outputDirectory>${project.build.directory}/../utils</outputDirectory>
</configuration>

This will successfully download the file from nexus an copy it to a utils folder inside the reactor level. Again, I'd prefer it went into the main plugin folder instead.

Here's the exec of that downloaded jar:

                        <configuration>
                            <executable>java</executable>
                            <arguments>
                                <argument>-Dloader.path=${project.build.directory}/${project.build.finalName}/${artifactId}.jar</argument>
                                <argument>-Dserver.port=10080</argument>
                                <argument>-jar</argument>
                                <argument>${project.build.directory}/../utils/my-service.jar</argument>
                            </arguments>
                            <includeProjectDependencies>false</includeProjectDependencies>
                        </configuration>

You can see we're passing in a path to the plugin's jar, which is in plugin-project-main/target ... With the above configuration I'm getting a reference to the reactor's jar instead (which doesn't exist).

So is there a way to dynamically get that folder of the main module in the reactor? If I can get that, then I can either put the utils into the module's folder (preferred) or at least pass the correct path to the util when it gets executed.

1

There are 1 best solutions below

0
TechGnome On

Well I found the answer to my problem - using an activation. I was able to add an activation to the profile to look for a specific directory (which will only exist in the main module structure.)

<activation>
  <activeByDefault>false</activeByDefault>
  <file>
    <exists>
      target/classes/content-root
    </exists>
  </file>
</activation>

The file that's downloaded to the utils folder now only does it once.

It's now lead to another problem, but that's for another thread.