【问题标题】:In Maven how do I copy files using the wagon plugin?在 Maven 中,如何使用 wagon 插件复制文件?
【发布时间】:2011-06-09 10:13:52
【问题描述】:

总结:如何使用 Maven 将一些生成的文件复制到网络服务器(例如 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。首先,我只想将文件复制到网络服务器提供的本地目录中。稍后我想远程复制它们,可能使用 ftp。有人可以举一个例子来说明我需要添加到pom.xml 以使本地副本正常工作(希望还有一个 ftp 示例?)。我在文档中找不到它。从阅读中我想我可能还需要Wagon Maven File Provider,但由于这似乎几乎没有文档我不确定。

【问题讨论】:

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


    【解决方案1】:

    Wagon 提供商只是为了提供额外的网络协议支持(例如 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

    【讨论】:

    • 嗨,您能否扩展您的答案以将副本显示到本地服务器?文档对于上传插件来说似乎也不是很好。
    【解决方案2】:

    最后我没有使用 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 类型,并可能根据需要添加 wagon 扩展。

    【讨论】:

    • 您不应该使用 wagon-maven-plugin 来执行此操作(请参阅 stackoverflow.com/questions/5493522/…)。 wagon-maven-plugin 和 maven-upload-plugin 都不是来自 org.apache.maven,它们来自 org.codehaus.mojo 和 com.atlassian.maven.plugins
    • 感谢您提供的信息 - 感觉很奇怪。我现在可能会坚持使用它,但我认为我现在宁愿依赖 codehaus 而不是 atlassian。我会继续寻找更好的解决方案
    • @Tristan、codehaus 和 maven 是好朋友;)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    相关资源
    最近更新 更多