从 pom.xml 收集依赖信息

Collect dependency information from pom.xml

我想打印一个文件,其中包含我 pom.xml 中所有依赖项及其版本的列表。 (无需进入每个依赖项内部)

我的最终目标是将依赖项+版本信息列在一个文件中,应用程序可以在运行时读取该文件,并通过 "version info" link 在网页上显示。

我发现有一个 maven dependecy plugin | dependency:list,据我所知,它应该做我想做的事情。我还管理它来打印输出文件,但它主要包含乱码。我能理解的是,它只是列出了项目中的包。 (无版本信息)

谁能解释一下如何正确使用这个插件,或者它是否能完成我需要它做的事情。 My configuration 与他们的使用说明相同,但如果尝试使用任何可选参数,它总是失败。

看起来它现在做的是对的。 'target/classes'中打印了一个版本文件,我在运行时从应用程序中查看时可以找到它。

在pom.xml(在build/plugins):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>createVersionInfo</id>
            <phase>compile</phase>
            <goals>
                <goal>list</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.company.project</groupId>
                        <artifactId>app-name</artifactId>
                        <version>${project.version}</version>
                        <type>war</type>
                        <overWrite>true</overWrite>
                    </artifactItem>
                </artifactItems>
                <sort>true</sort>
                <includeScope>compile</includeScope>
                <outputFile>target/classes/dependency_versions.txt</outputFile>
            </configuration>
        </execution>
    </executions>
</plugin>

测试”未按预期运行。它排除了一切。我认为 includeScope 的解释以某种方式解释了这一点。

An Empty string indicates all scopes (default). The scopes being interpreted are the scopes as Maven sees them, not as specified in the pom. In summary:

runtime  - scope gives runtime and compile dependencies,
compile  - scope gives compile, provided, and system dependencies,
test     - (default) scope gives all dependencies,
provided - scope just gives provided dependencies,
system   - scope just gives system dependencies.

java 端的这段代码几乎可以完成所需的工作:

InputStream in = this.getClass().getResourceAsStream("/dependency_versions.txt"); // Finds the file from classpath
String content = "";
// Java7 AutoCloseable
try (Scanner scan = new Scanner(in)) {
    scan.useDelimiter("\Z"); // Found this from another SO-thread, enables to read in the whole file at once
    content = scan.next();
}
// Iterate the content and collect the information
// \n = line change
for (String row : content.split("\n")) {
    row = row.trim();
    // Basically removes the 1st two lines of the file that are not wanted
    if (StringUtils.isEmpty(row) || row.endsWith(":")) {
        continue;
    }
    StringTokenizer tokenizer = new StringTokenizer(row, ":");
    String package = tokenizer.nextToken();
    String dependency = tokenizer.nextToken();
    String type = tokenizer.nextToken();
    String version = tokenizer.nextToken();
    String scope = tokenizer.nextToken();
}

我实际上正在填充我自己的 TO 列表以在 JSF 中显示信息,但我省略了 simplicity/readability 的那部分。

...实际上让我想到为什么我要一次读取整个文件而不是迭代它并一次读取一行,但我会把它留给每日 WTF。 ;)