Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a file during maven build phase

Tags:

java

maven

war

My situation is:

  • I have a Maven project, I have my java classes in /app/src/main/java, my resources in /app/src/main/resources and my webapp files in /app/src/main/webapp
  • I have a javascript file in /common/script.js

Now what I want is to include (copy) the javascript file to the war file during the build phase of maven. To be precise, I want the script.js to land in /js/ directory of the war archive, just as it was placed in /app/src/main/webapp/js before starting the build.

I need this to share one version of resource files among many web-apps.

Kind regards, Q.

like image 466
Queequeg Avatar asked Dec 06 '11 08:12

Queequeg


2 Answers

You could do something like this, as documented here.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <configuration>
          <webResources>
            <resource>
              <!-- this is relative to the pom.xml directory -->
              <directory>../common</directory>
              <targetPath>/js</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>
like image 197
Raghuram Avatar answered Sep 22 '22 10:09

Raghuram


You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element.

Check

"maven-resources-plugin"

like image 45
Bala Avatar answered Sep 21 '22 10:09

Bala