In Maven how do I copy files using the wagon plugin?

Nick Fortescue picture Nick Fortescue · Jun 9, 2011 · Viewed 14.3k times · Source

Summary: How do I copy some generated files into a webserver (eg IIS or Apache) directory using Maven?

Details: I have a working application that builds in Maven. I've managed to get it building using the webstart-maven-plugin which produces all the needed files (.jar and .jnlp) in a directory target/jnlp. It also creates a zip file with them all in at target/foo-1.0.zip.

At the moment the webstart plugin does not have a deploy goal - the request for it has ended up on the FAQ (question 3). It may be implemented in future, but the suggestion for the time being is to use wagon-maven-plugin.

I've never used Wagon. To start with I'd like to just copy the files to a local directory served up by a webserver. Later I'd like to copy them remotely, probably using ftp. Can someone give an example to what I need to add to the pom.xml to get the local copy working (and hopefully an ftp example too?). I can't find it in the documentation. From reading I think I might also need the Wagon Maven File Provider but as this seems to have almost no documentation I'm not sure.

Answer

Tristan picture Tristan · Jun 9, 2011

Wagon providers are only there to provide additional network protocol supports (such as FTP).

If you want to copy file to a webserver (local or distant) you can use Maven upload plugin :

...
<plugin>
    <groupId>com.atlassian.maven.plugins</groupId>
    <artifactId>maven-upload-plugin</artifactId>
</plugin>
...

In parent pom :

            <plugin>
                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>maven-upload-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <resourceSrc>
                        ${project.build.directory}/${project.build.finalName}.${project.packaging}
                    </resourceSrc>
                    <resourceDest>${jboss.deployDir}</resourceDest>
                    <serverId>${jboss.host}</serverId>
                    <url>${jboss.deployUrl}</url>
                </configuration>
            </plugin>

And to configure parameters in a smart way, I use maven profiles (in parent pom) :

<profiles>
    <!-- local deployment -->
    <profile>
        <id>developpement</id>
        <properties>
            <jboss.host>localhost</jboss.host>
            <jboss.deployDir>appli/jboss-4.0.4.GA/server/default/deploy/</jboss.deployDir>
            <jboss.deployUrl>file://C:/</jboss.deployUrl>
        </properties>
    </profile>
    <!-- distant deployment -->
    <profile>
        <id>validation</id>
        <properties>
            <jboss.host>ENV_val</jboss.host>
            <jboss.deployDir>/home/envval/jboss/server/default/deploy/</jboss.deployDir>
            <jboss.deployUrl>scp://PROJECT_LAN_HOST</jboss.deployUrl>
        </properties>
    </profile>
</profiles>

I've created an "ant launcher", to use it by clicking under Eclipse ant view :

<target name="copy war to JBoss local" description="Copy war to local JBoss">
    <maven goal="upload:upload" options="-Pdeveloppement" />
</target>

But you can simply run it on a command line :

mvn upload:upload -Pdeveloppement

EDIT : By the way, for distant deployment, you may need a login password for scp to work. You have to add them to you Maven settings.xml file :

<settings>
  ...
  <servers>
    <server>
      <id>ENV_val</id>
      <username>login</username>
      <password>password</password>
    </server>
  </servers>
  ...
</settings>

EDIT: You'll need to add the Atlassian repository:

    <pluginRepositories>
    <pluginRepository>
        <id>Atlassian</id>
        <url>https://maven.atlassian.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>   

EDIT: depending upong the remote protocol you'll have to add wagon extensions, see Uploading a directory using sftp with Maven