简体   繁体   中英

Extract Maven Dependency Version from POM

Given that I have Dependency groupId and artifactId, how do I extract the version for this dependency from the pom.xml file?

Example: I want to be able to extract the dependency version below if I query using the artifactId and groupId.

        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.6</version>
        </dependency>

I want to assign the version to a global environment variable ie DEP_VERSION=1.6

It is an xml. Trivially you could make a parsing of the file.

You can use the Maven Dependency Plugin :

mvn dependency:tree -Dincludes=com.google.guava:guava

It will output something like:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MyProject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ project-server ---
[INFO] com.company.project:project-server:jar:0.0.1-SNAPSHOT
[INFO] \- com.google.guava:guava:jar:19.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.016 s
[INFO] Finished at: 2016-04-06T16:58:55+02:00
[INFO] Final Memory: 21M/437M
[INFO] ------------------------------------------------------------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM