【问题标题】:How to get a list of all dependency JARs in a parameter?如何获取参数中所有依赖项 JAR 的列表?
【发布时间】:2012-11-03 19:09:03
【问题描述】:

我正在为 Talend Open Studio 开发一个插件;该平台的组件架构需要在组件描述符 XML 文件中声明所有外部 JAR,格式如下:

<IMPORT MODULE="commons-collections-3.2.1.jar" NAME="commons-collections-3.2.1" 
    REQUIRED="true"/>

我使用 Maven 依赖插件来管理所有这些外部 JAR

有没有办法在一个列表或其他东西中获取所有依赖项名称?这样我可以构建所需的字符串(也许使用 antcontrib 任务),填充 ${parameter} 并最终使用 maven-replacer-plugin 将其添加到 XML 文件中吗?

【问题讨论】:

  • 使用依赖插件?可以生成类路径、依赖树等。
  • 好的,但是,例如,我不知道如何从类路径字符串中创建 ${parameter}...
  • 拆分它并使用任何一种 XML/模板/任何机制来生成 XML?创建一个使用该 XML 文件的任何人都可以理解的属性文件?等等。

标签: maven talend ant-contrib maven-dependency-plugin


【解决方案1】:

最简单的解决方案是通过buld-classpath goal 使用maven-dependency-plugin。可以为这个目标提供补充参数,以将结果放入文件中,例如:

mvn dependency:build-classpath -Dmdep.outputFile=classpath.out

【讨论】:

  • 如果直接在命令行上使用,那么你需要使用mdep.outputFile 而不是outputFile
【解决方案2】:

好的,我部分解决了这种应该有一些限制的方法:

<plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
        <dependencies>
            <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
            <exclusions>
                <exclusion>
                    <groupId>ant</groupId>
                    <artifactId>ant</artifactId>
                </exclusion>
            </exclusions>
            </dependency>
            <dependency>
                <groupId>ant</groupId>
                <artifactId>ant-nodeps</artifactId>
                <version>1.6.5</version>
            </dependency>
        </dependencies>
        <executions>
          <execution>
            <phase>package</phase>
            <id>copy-resources</id>
            <configuration>
              <exportAntProperties>true</exportAntProperties>
              <tasks>
                <!-- add the ant tasks from ant-contrib -->
                <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.plugin.classpath"/>
                <var name="import.set" value=""/> 
                <for param="file">
                    <path>
                        <fileset dir="${project.build.directory}" includes="*.jar"/>
                    </path>
                    <sequential> 
                        <var name="basename" unset="true"/> 
                        <basename file="@{file}" property="basename"/>
                        <var name="filenames" value="${basename}"/>
                        <var name="import.clause" value='&lt;IMPORT MODULE="${filenames}" NAME="${filenames}" REQUIRED="true"/&gt;'/>
                        <var name="import.set" value="${import.clause}${line.separator}${import.set}" />
                    </sequential> 
                </for>
                <property name="import.jar" value="${import.set}"/> 
                <echo>${import.jar}</echo>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

还有一些问题:即使 exportAntProperties 设置为 true,属性 ${import.jar} 在其他 maven 目标中的 ant taska 之外仍然不可用,而如果我切换到 maven-antrun-plugin 1.7 版本,“执行 ant 任务时出错:org.apache.tools.ant.launch.Locator.fromJarURI(Ljava/lang/String;)Ljava/lang/String;"抛出异常。还是没有线索……

【讨论】:

    猜你喜欢
    • 2017-12-27
    • 2013-06-28
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多