【问题标题】:obtain the location of a jar that is a dependency in maven获取一个 jar 的位置,它是 maven 中的一个依赖项
【发布时间】:2012-03-05 13:47:18
【问题描述】:

我有一个项目使用一些遗留脚本来处理源代码。我无法摆脱它,所以我想从 maven 中调用它。

问题是我需要将 jar 文件的位置作为参数传递。我已将此 jar 文件列为我的 pom.xml 中的依赖项。有没有办法可以将 jar 文件的绝对位置传递给这个脚本?

【问题讨论】:

    标签: maven


    【解决方案1】:

    这绝不是理想的,但您可以从 maven 调用您的脚本,并将其作为参数传递:

    ${settings.localRepository}/<path to artifact>
    

    artifact 的路径是由你想要的组 id 和 artifact id 组成的路径。例如,如果您想引用 maven-jar-plugin 2.2 版,您可以使用:

    ${settings.localRepository}/org/apache/maven/plugins/maven-jar-plugin/2.2/maven-jar-plugin-2.2.jar
    

    【讨论】:

    • 是的,您所做的确实非常规,所以这根本不理想。更好的计划是对那些遗留脚本进行 mavenize,这样你就不再需要它们了 ;)
    【解决方案2】:

    我更喜欢Pascal Thivent's answer 来回答类似的问题。您可以使用 ${maven.dependency.junit.junit.jar.path} 表示法来引用依赖项。 Pascal 在他的回答中包含了一个示例 pom:

    <?xml version="1.0" encoding="UTF-8"?>
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.stackoverflow</groupId>
      <artifactId>q2359872</artifactId>
      <version>1.0-SNAPSHOT</version>
      <name>q2359872</name>
      <properties>
        <my.lib>${maven.dependency.junit.junit.jar.path}</my.lib>
      </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <phase>process-resources</phase>
                <configuration>
                  <tasks>
                    <echo>${my.lib}</echo>
                  </tasks>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    【讨论】:

    • 显然这仅在使用 antrun 插件时有效,这不是我的情况
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    相关资源
    最近更新 更多