在Maven中如何使用wagon插件复制文件?

时间:2011-06-09 10:13:52

标签: maven wagon maven-wagon-plugin maven-webstart-plugin

摘要:如何使用Maven将一些生成的文件复制到Web服务器(例如IIS或Apache)目录中?

详细说明: 我有一个在Maven中构建的工作应用程序。我设法使用webstart-maven-plugin来构建它,它在目录target/jnlp中生成所有需要的文件(.jar和.jnlp)。它还会在target/foo-1.0.zip处创建一个zip文件。

目前,webstart插件没有deploy目标 - 对FAQ (question 3)的请求已经结束。它可能会在将来实施,但目前的建议是使用wagon-maven-plugin

我从未使用过Wagon。首先,我想将文件复制到由Web服务器提供的本地目录中。后来我想远程复制它们,可能使用ftp。有人可以举例说明我需要添加到pom.xml以使本地副本正常工作(并希望也是一个ftp示例吗?)。我在文档中找不到它。从阅读开始,我想我可能也需要Wagon Maven File Provider,但因为这似乎几乎没有文件我不确定。

2 个答案:

答案 0 :(得分:6)

旅行车提供商只在那里提供额外的网络协议支持(例如FTP)。

如果要将文件复制到网络服务器(本地或远程),可以使用Maven上传插件:

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

在父母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>

要以智能方式配置参数,我使用maven配置文件(在父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>

我创建了一个“ant启动器”,通过在Eclipse ant视图下单击来使用它:

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

但您可以在命令行上运行它:

mvn upload:upload -Pdeveloppement

编辑:顺便说一句,对于远程部署,您可能需要登录密码才能使用scp。您必须将它们添加到Maven settings.xml文件中:

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

编辑:您需要添加Atlassian存储库:

    <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>   

编辑:根据您需要添加旅行车扩展程序的远程协议,请参阅Uploading a directory using sftp with Maven

答案 1 :(得分:3)

最后我没有使用Maven上传插件 - 它看起来有点受限,而不是主要maven发行版的一部分。我按照建议使用了maven wagon插件。这是我能做到的最简单的pom。希望其他人会发现它很有用,因为我找不到任何类似的东西。

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>wagon-maven-plugin</artifactId>
    <version>1.0-beta-3</version>
    <configuration>
      <fromDir>${project.build.directory}/jnlp</fromDir>
      <includes>*</includes>
      <url>file://c:/inetpub/wwwroot</url>
      <toDir>jnlp</toDir>
    </configuration>
  </plugin>

对于远程分发,您只需更改URL类型,并根据需要添加货车扩展。

相关问题