【问题标题】:Maven: Install in local repository a jar using its ftp addressMaven:使用其 ftp 地址在本地存储库中安装 jar
【发布时间】:2017-12-11 10:09:15
【问题描述】:

我的 Maven 本地存储库位于 Windows 机器上,我正在尝试使用 eclipse 使用 jar 的 ftp 地址在此存储库中安装 jar(jar 位于 linux 机器上)。这是我的settings.xml 和pom.xml

settings.xml

   <server>
    <id>{server_address}</id>
    <username>username</username>
    <password>password</password>
    <configuration>
        <endpointChecking>false</endpointChecking>
    </configuration>
  </server>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven.bcone</groupId>
<artifactId>ArtifactName</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Sample</name>
<properties>
    <ics.version>17.4.5</ics.version>
    <ftp.dir>//sftp://username@{server_address}</ftp.dir>
</properties>

<dependencies>
    <dependency>
        <groupId>com.oracle.ics</groupId>
        <artifactId>cpi_omcs.jar</artifactId>
        <version>${ics.version}</version>
    </dependency>

</dependencies>
<build>
    <finalName>BuildName</finalName>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <id>install-cpi_omcs</id>
                    <phase>clean</phase>
                    <configuration>
                        <file>${ftp.dir}/home/steve/myJars/cpi_omcs.jar</file>
                        <groupId>com.oracle.ics</groupId>
                        <artifactId>cpi_omcs</artifactId>
                        <version>${ics.version}</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
    <repositories>
        <repository>
            <id>{server_address}</id>
            <name>158_bcone</name>
            <url>sftp://username@{server_address}</url>
            <layout>default</layout>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </releases>
        </repository>
    </repositories>
  </project>

运行 mvn:clean 后,出现以下错误

[错误] 无法在项目 BconeAdapter 上执行目标 org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file (install-cpi_omcs):指定文件 '\\sftp:\username @{server_addres}\home\steve\myJars\cpi_omcs.jar' 不存在 -> [帮助 1]

jar 文件存在于上述位置,但 maven 无法找到它。我在这里错过了什么?

【问题讨论】:

  • 你也设置{path_to_jar_on_server}吗?
  • 不,我写它只是为了代表目的。 {path_to_jar_on_server} 实际上是服务器上的有效路径。让我更改它以避免混淆。
  • 你有 ftp.dir = //sftp://username@{server_address}?领先的// 有效/需要吗?为什么那些反斜杠 `\` 异常? Windows 会转换它们吗?
  • 1.如果我没有使用前导 // 斜杠,那么它会解释来自 Eclipse 工作区的路径。例如如果我在 Windows 上的工作区位于:D:/steve/java then without// 斜杠解释的路径是D:/steve/java/sftp:/username@{server_address} 2. 我猜是 Windows 本身正在转换异常中的反斜杠。
  • 为什么使用 ftp 作为安装协议?默认使用 http(s) 将工件部署到存储库管理器...您为什么要这样做?

标签: java eclipse maven ftp


【解决方案1】:

我可以使用这些 maven 插件来做到这一点

<properties>
    <ics.version>18.1.3</ics.version>
    <temp.dir>./temp/</temp.dir>
    <ftp.dir>scp://username:password@server-address</ftp.dir>
    <ics.dir>/u01/app/oracle/lib</ics.dir>
</properties>

    <build>
    <finalName>MyProject</finalName>
    <plugins>

        <!-- DOWNLOAD THE JAR FILES TO A TEMP FOLDER ON THE LOCAL MACHINE -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>wagon-maven-plugin</artifactId>
            <version>1.0</version>

            <executions>
                <execution>
                    <id>download-ftp-suite</id>
                    <configuration>
                        <url>${ftp.dir}:${ics.dir}</url>
                        <toDir>${temp.dir}</toDir>
                        <includes>
                            */**/*.jar
                        </includes>
                    </configuration>
                    <goals>
                        <goal>download</goal>
                    </goals>
                </execution>
              </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>

                <execution>
                    <id>install-one-of-the-downloaded-jars</id>
                    <phase>site</phase>
                    <configuration>
                        <file>${temp.dir}one-of-the-downloaded-jars.jar</file>
                        <groupId>com.oracle.ics</groupId>
                        <artifactId>one-of-the-downloaded-jars</artifactId>
                        <version>${ics.version}</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
               </executions>
        </plugin>
     </plugins>


    <extensions>
        <!-- Enabling the use of SSH IN ACCESSING FTP/SCP -->
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>3.0.0</version>
        </extension>
    </extensions>
</build>

通过这些更改,无需在 settings.xml 中添加服务器详细信息

【讨论】:

    【解决方案2】:

    也许您应该使用maven-wagon。我不确定maven-install-plugin 是否可以处理网络路径。

    其余部分是从 Apaches 页面复制和粘贴的全部内容,因为它是使解决方案正常工作所需的最少基本信息

    来自Apache maven deploy plugin page

    为了使用 FTP 部署工件,您必须首先指定使用 POM 的 distributionManagement 元素中的 FTP 服务器 以及在你的构建元素中指定一个扩展,它将拉 在使用 FTP 部署所需的 FTP 工件中:

    <distributionManagement>
       <repository>
         <id>ftp-repository</id>
         <url>ftp://repository.mycompany.com/repository</url>
       </repository>
    </distributionManagement>
    
    <build>
       <extensions>
          <extension>
            <groupId>org.apache.maven.wagon</groupId>
             <artifactId>wagon-ftp</artifactId>
             <version>*version*</version>
          </extension>
       </extensions>
    </build>
    

    您的 settings.xml 将包含一个服务器元素,其中该元素的 id 元素匹配上面 POM 中指定的 FTP 存储库的 id:

    <settings>
       ...
       <servers>
          <server>
             <id>ftp-repository</id>
             <username>user</username>
             <password>pass</password>
          </server>
      </servers>
      ...
    </settings>
    

    当然,您应该确保您可以登录到指定的 在尝试使用 Maven 进行部署之前手动 FTP 服务器。一次 您已验证一切都已正确设置您现在可以 使用 Maven 部署您的工件:

    mvn deploy
    

    【讨论】:

    • 你确定它适用于安装 jar。 bcoz 解决方案正在讨论 部署 jar 到远程位置
    • 当我测试它似乎做正确的事情,所以复制 jar 和 pom 的东西来更正提供的 url 下的包/版本目录。但是:似乎它不支持sftp 仅ftp。
    • 我收到此错误:[ERROR] Failed to execute goal on project BconeAdapter: Could not resolve dependencies for project com.maven.bcone:BconeAdapter:jar:0.0.1-SNAPSHOT: Failure to find com.oracle.ics:cpi_omcs.jar:jar:17.4.5 in https://repo.maven.apache.org/maven2 was cached in the local repository
    • 认为没有关系。为什么您要安装相同的工件作为依赖项?为什么安装项目将其作为依赖项?尝试在没有它的情况下运行。
    • 我正在尝试将依赖项从远程 ftp 位置移动到我的本地系统存储库,即我正在将 jar 从 Unix 机器移动到我的 Windows 机器,并将它们安装在 Windows 机器上的本地存储库中。部署项目会将项目 jar 从 Windows 移动到 UNIX,这不是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 2018-05-23
    • 2022-08-14
    相关资源
    最近更新 更多