Tomas F
Tomas F

Reputation: 7596

How to get the actual output file name from maven

I try to configure the maven ant plugin to copy the built artifact to a custom location:

<plugins>
  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <phase>install</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <tasks>

            <copy verbose="true" 
              file="target/${project.build.finalName}.${project.packaging}"
              tofile="${user.home}/tmp/test/${project.build.finalName}.${project.packaging}"/>

          </tasks>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

This works fine, as long as the packaging is one of the standard ones... but if the packaging of the project is "bundle" (which generates a .jar), then the ${project.packaging} variable is "bundle" and the actual file ends with ".jar" = the copy fails.

How can I get the "real" name of the file that is put into the output directory?

Upvotes: 5

Views: 13854

Answers (3)

qxo
qxo

Reputation: 1694

In ant build.xml using:

${project.build.directory}/${project.artifactId}-${project.version}.jar

Upvotes: 11

Ludwig Magnusson
Ludwig Magnusson

Reputation: 14399

One way is to create a property called packaging-suffix or something similair. In all your projects (or in the super pom if you have one) you can define this property to have the value ${project.packaging} but in the bundle projects your define the value as jar. You then reference the property in your antrun plugin congfiguration.

Depending on how your project(s) are set up, this may or may not be a good solution.

I.e:

<properties>
  <packaging-suffix>${project.packaging}</packaging-suffix>
</properties>

vs

<properties>
  <packaging-suffix>jar</packaging-suffix>
</properties>

then

<copy verbose="true"  
  file="target/${project.build.finalName}.${packaging-suffix}"
  tofile="${user.home}/tmp/test/${project.build.finalName}.${packaging-suffix}"/>

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328624

There is no way to do that reliably. For example, a POM can have several artifacts as result (binary JAR, sources JAR, test binary JAR, test sources JAR). Which one of them is the correct one to copy?

Possible solutions:

  1. Replace ${project.packaging} with jar. Should work in most cases.
  2. Use a file set instead of a hard coded file name to let Ant figure out the names based on patterns.

Upvotes: 3

Related Questions