Ikthiander Ikthiander - 1 month ago 10
Java Question

how to copy a file to tomcat webapp folder using maven?

i want to copy a resource file from src/main/resources to cargo tomcat in /target/tomcat6x/container/webapps using maven. i tried using maven-resources-plugin but not having any success. could any kind person who knows maven show me the right direction.

i tried this:

<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0.5</version>
<configuration>
<wait>false</wait>
<container>
<containerId>tomcat6x</containerId>
<zipUrlInstaller>
<url>
http://mirrors.enquira.co.uk/apache/tomcat/tomcat-6/v6.0.30/bin/apache-tomcat-6.0.30.zip
</url>
<installDir>${installDir}</installDir>
</zipUrlInstaller>
<output>
${project.build.directory}/tomcat6x.log
</output>
<log>${project.build.directory}/cargo.log</log>
</container>
<configuration>
<files>
<copy>
<file>src/main/resources/datasource.properties</file>
<!--tofile>${project.build.directory}/tomcat6x/container/webapps/datasource.properties</tofile-->
<todir>${project.build.directory}/tomcat6x/container/webapps</todir>
</copy>
</files>
<home>
${project.build.directory}/tomcat6x/container
</home>
<properties>
<cargo.logging>high</cargo.logging>
<cargo.servlet.port>8081</cargo.servlet.port>
</properties>
</configuration>
</configuration>


<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>configure</goal>
<goal>start</goal>
<goal>deploy</goal>
</goals>
<configuration>
<deployer>
<deployables>
<deployable>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<type>war</type>
<pingURL>http://localhost:8081/charmweb-0.0.1-SNAPSHOT/</pingURL>
<pingTimeout>180000</pingTimeout>
<properties>
<context>charmweb-0.0.1-SNAPSHOT</context>
</properties>
</deployable>
</deployables>
</deployer>
</configuration>
</execution>

<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>


but the file is not copying before the server starts and the application gets pinged. any one knows how to use it properly??

thanks in advance

Answer Source

You can use the maven-antrun-plugin for this and execute an ant copy task:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <target>
                    <copy file="src/main/resources/fileToCopy"
                        tofile="${project.build.directory}/tomcat6x/container/webapps/fileToCopy" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>