【问题标题】:Maven and adding JARs to system scopeMaven 并将 JAR 添加到系统范围
【发布时间】:2012-06-11 16:53:16
【问题描述】:

我的 Android 项目中有一个 JAR,我希望将它添加到最终的 APK 中。 好的,我开始:

    <dependency>
        <groupId>com.loopj.android.http</groupId>
        <artifactId>android-async-http</artifactId>
        <version>1.3.2</version>
        <type>jar</type>
        <scope>system</scope>
        <systemPath>${project.basedir}/libs/android-async-http-1.3.2.jar</systemPath>
    </dependency>

但是当我运行 mvn package 时,我收到了警告:

[WARNING] Some problems were encountered while building the effective model for **apk:1.0
[WARNING] 'dependencies.dependency.systemPath' for com.loopj.android.http:android-async-http:jar should not point at files within the project directory, ${project.basedir}/libs/android-async-http-1.3.2.jar will be unresolvable by dependent projects @ line 36, column 25

在最终的 APK 中没有 JAR。

我该如何解决这个问题?

【问题讨论】:

  • 您不能以这种方式使用系统范围。使用 install:install-file.
  • @bmargulies 你能说一下这个范围的用途吗?
  • 我切换到 gradle 并且不再有这些头痛了,除了现在我正在尝试使用带有 maven 的开源库并暂时破解一个 jar(这在 gradle 中很容易,但很难在 Maven 中)。
  • 这个问题讨论了如何避免在 Maven 中使用系统范围:stackoverflow.com/questions/3642023/…
  • 关于“系统”范围的官方文档:maven.apache.org/guides/introduction/…

标签: maven


【解决方案1】:

您需要add the jar 到您的本地 Maven 存储库。或者(更好的选择)指定适当的存储库(如果存在),以便它可以由 maven 自动下载

在任何一种情况下,从依赖项中删除 &lt;systemPath&gt; 标记

【讨论】:

  • 我看过那篇文章,但我希望不要在我想要构建该项目的每台计算机上执行maven install(不幸的是,我没有在 repos 中找到这个 JAR)。谢谢! :)
  • 它可以作为构建的一部分编写脚本。
【解决方案2】:

使用存储库管理器并将此类 jar 安装到其中。这完全解决了您的问题,适用于您网络中的所有计算机。

【讨论】:

  • 我们计划明天或后天在本地服务器上运行一个 repo,但在此之前我应该​​以其他方式解决这个问题。
  • 正如@efpies 提到的,当开发人员没有创建回购管理器的权限/能力时,这可能是一个天上掉馅饼的答案。
  • 现在,如果你安装了 docker,你只需要docker run -d -p 8081:8081 --name nexus sonatype/nexus3 -- 详情请参阅hub.docker.com/r/sonatype/nexus3
【解决方案3】:

系统范围仅用于处理“系统”文件;位于某个固定位置的文件。 /usr/lib${java.home} 中的文件(例如 tools.jar)。它的设计初衷不是支持您项目中的各种 .jar 文件。

作者故意拒绝使路径名扩展正常工作以阻止您。因此,短期内你可以使用install:install-file 安装到本地repo,然后有一天使用repo manager 共享。

【讨论】:

    【解决方案4】:

    我不知道真正的原因,但 Maven 推动开发人员将所有库(也自定义)安装到一些 maven 存储库中,所以 scope:system 不太受欢迎,一个简单的解决方法是使用 maven-install-plugin

    遵循用法:

    这样写你的依赖

    <dependency>
        <groupId>com.mylib</groupId>
        <artifactId>mylib-core</artifactId>
        <version>0.0.1</version>
    </dependency>
    

    然后,添加 maven-install-plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
        <executions>
            <execution>
                <id>install-external</id>
                <phase>clean</phase>
                <configuration>
                    <file>${basedir}/lib/mylib-core-0.0.1.jar</file>
                    <repositoryLayout>default</repositoryLayout>
                    <groupId>com.mylib</groupId>
                    <artifactId>mylib-core</artifactId>
                    <version>0.0.1</version>
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                </configuration>
                <goals>
                    <goal>install-file</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    注意phase:clean,要将您的自定义库安装到您的存储库中,您必须运行mvn clean,然后运行mvn install

    【讨论】:

    • 为什么不使用&lt;phase&gt;process-resources&lt;/phase&gt; 而不是&lt;phase&gt;clean&lt;/phase&gt;。 process-resources 阶段看起来更适合这种场景,它总是在编译阶段之前调用。
    • 在第一次安装中,您确定可以通过“构建生命周期”中流程资源之前的“验证”阶段吗? ; ),“干净的生命周期”首先是“构建的生命周期”,它没有任何验证的依赖,tutorialspoint.com/maven/maven_build_life_cycle.htm
    • 它可以工作,但是你如何安装几个依赖项?
    • Stackoverflow 应该添加一项功能,允许社区覆盖 OP 选择的正确答案,因为恕我直言,这个答案应该是公认的! :)
    • 正如其他人评论的那样,绑定到clean 阶段非常具有误导性,它不是默认生命周期的一部分,并且扭曲了clean 的含义。此外,在默认生命周期中使用阶段(例如validateprocess-resources)的建议更改将在多模块情况下失败,因为聚合器尝试解决依赖关系,之前为子模块执行的任何自定义目标。
    【解决方案5】:

    试试这个配置。它对我有用:

    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <warSourceDirectory>mywebRoot</warSourceDirectory>
            <warSourceExcludes>source\**,build\**,dist\**,WEB-INF\lib\*,
                WEB-INF\classes\**,build.*
            </warSourceExcludes>
            <webXml>myproject/source/deploiement/web.xml</webXml>
            <webResources>
                <resource>
                    <directory>mywebRoot/WEB-INF/lib</directory>
                    <targetPath>WEB-INF/lib</targetPath>
                    <includes>
                            <include>mySystemJar1.jar.jar</include>
                             <include>mySystemJar2.jar</include>
                       </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>
    

    【讨论】:

      【解决方案6】:

      mvn install:install-file -DgroupId=com.paic.maven -DartifactId=tplconfig-maven-plugin -Dversion=1.0 -Dpackaging=jar -Dfile=tplconfig-maven-plugin-1.0.jar -DgeneratePom=true

      将 jar 安装到本地存储库。

      【讨论】:

      • 这比直接通过pom添加要麻烦。
      【解决方案7】:
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <includeSystemScope>true</includeSystemScope>
          </configuration>
      </plugin>
      

      试试这个。

      【讨论】:

        【解决方案8】:

        感谢 Ging3r 我得到了解决方案:

        按照以下步骤操作:

        1. 不要在依赖标签中使用。在 pom.xml 文件的依赖项标签中使用以下内容::

          <dependency>
          <groupId>com.netsuite.suitetalk.proxy.v2019_1</groupId>
          <artifactId>suitetalk-axis-proxy-v2019_1</artifactId>
          <version>1.0.0</version>
          </dependency>
          <dependency>
              <groupId>com.netsuite.suitetalk.client.v2019_1</groupId>
              <artifactId>suitetalk-client-v2019_1</artifactId>
              <version>2.0.0</version>
          </dependency>
          <dependency>
              <groupId>com.netsuite.suitetalk.client.common</groupId>
              <artifactId>suitetalk-client-common</artifactId>
              <version>1.0.0</version>
          </dependency>
          
        2. 在 pom.xml 文件的 plugins 标签中使用以下代码:

              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-install-plugin</artifactId>
                  <version>2.5.2</version>
                  <executions>
                      <execution>
                          <id>suitetalk-proxy</id>
                          <phase>clean</phase>
                          <configuration>
                              <file>${basedir}/lib/suitetalk-axis-proxy-v2019_1-1.0.0.jar</file>
                              <repositoryLayout>default</repositoryLayout>
                              <groupId>com.netsuite.suitetalk.proxy.v2019_1</groupId>
                              <artifactId>suitetalk-axis-proxy-v2019_1</artifactId>
                              <version>1.0.0</version>
                              <packaging>jar</packaging>
                              <generatePom>true</generatePom>
                          </configuration>
                          <goals>
                              <goal>install-file</goal>
                          </goals>
                      </execution>
                      <execution>
                          <id>suitetalk-client</id>
                          <phase>clean</phase>
                          <configuration>
                              <file>${basedir}/lib/suitetalk-client-v2019_1-2.0.0.jar</file>
                              <repositoryLayout>default</repositoryLayout>
                              <groupId>com.netsuite.suitetalk.client.v2019_1</groupId>
                              <artifactId>suitetalk-client-v2019_1</artifactId>
                              <version>2.0.0</version>
                              <packaging>jar</packaging>
                              <generatePom>true</generatePom>
                          </configuration>
                          <goals>
                              <goal>install-file</goal>
                          </goals>
                      </execution>
                      <execution>
                          <id>suitetalk-client-common</id>
                          <phase>clean</phase>
                          <configuration>
                              <file>${basedir}/lib/suitetalk-client-common-1.0.0.jar</file>
                              <repositoryLayout>default</repositoryLayout>
                              <groupId>com.netsuite.suitetalk.client.common</groupId>
                              <artifactId>suitetalk-client-common</artifactId>
                              <version>1.0.0</version>
                              <packaging>jar</packaging>
                              <generatePom>true</generatePom>
                          </configuration>
                          <goals>
                              <goal>install-file</goal>
                          </goals>
                      </execution>
                  </executions>
              </plugin>
          

        我包含来自 lib 文件夹的 3 个 jar:

        最后,使用mvn clean,然后使用mvn install或'mvn clean install',然后从目标文件夹或安装路径运行jar文件(参见mvn install日志):

        java -jar abc.jar

        注意:如果您在 jenkins 工作,请记住一件事,然后首先使用 mvn clean,然后使用 mvn clean install 命令为您工作,因为之前的代码 mvn clean install 命令存储缓存以供依赖。

        【讨论】:

          【解决方案9】:

          在这个线程之后,我能够配置安装插件来加载我的自定义 jar,但是在运行 mvn install 时插件没有看到我的 configuration

          我正在使用maven:3.6.3-jdk-8 docker 映像的基础maven-install-plugin:2.5.2

          我不完全理解this note in the documentation(在本节末尾),但您似乎可以给阶段目标一个执行ID,强制它使用您的配置:

          注意:元素内部的配置过去不同于外部的配置,因为它们不能从直接命令行调用中使用,因为它们仅在它们绑定到的生命周期阶段被调用时才应用。因此,您必须将配置部分移到执行部分之外,以将其全局应用于插件的所有调用。由于 Maven 3.3.1 不再是这种情况,因为您可以在命令行上指定直接插件目标调用的执行 ID。因此,如果你想从命令行运行上面的插件并且它是特定的 execution1 的配置,你可以执行:

          mvn myqyeryplugin:queryMojo@execution1

          我最后的工作 docker 命令:

          docker run -it --rm --name parser -v "$(shell pwd)":/usr/src/parser -w /usr/src/parser maven:3.6.3-jdk-8 mvn -X install:install-file@install-my-jar-file
          

          install-my-jar-file 是我的处决 ID &lt;execution&gt;&lt;id&gt;install-my-jar-file&lt;/id&gt;...

          【讨论】:

            猜你喜欢
            • 2011-02-05
            • 2011-04-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多