How to list active sub-modules in a Maven project?

前端 未结 8 1268
长发绾君心
长发绾君心 2020-12-24 13:26

I have a complex project where there are many directories that have POM files, but only some of which are sub-modules (possibly transitively) of a particular parent project.

相关标签:
8条回答
  • 2020-12-24 13:47
    mvn help:evaluate -Dexpression=project.modules
    
    mvn help:evaluate -Dexpression=project.modules[0]
    mvn help:evaluate -Dexpression=project.modules[1]
    
    IFS=$'\n'
    modules=($(mvn help:evaluate -Dexpression=project.modules | grep -v "^\[" | grep -v "<\/*strings>" | sed 's/<\/*string>//g' | sed 's/[[:space:]]//'))
    for module in "${modules[@]}"
    do
        echo "$module"
    done
    
    0 讨论(0)
  • 2020-12-24 13:47

    I don't have a direct answer to the question. But using some kind of "module path" as naming convention for the <name> of my modules works for me. As you'll see, this convention is self explaining.

    Given the following project structure:

    .
    ├── pom
    │   ├── pom.xml
    │   └── release.properties
    ├── pom.xml
    ├── samples
    │   ├── ejb-cargo-sample
    │   │   ├── functests
    │   │   │   ├── pom.xml
    │   │   │   └── src
    │   │   ├── pom.xml
    │   │   └── services
    │   │       ├── pom.xml
    │   │       └── src
    │   └── pom.xml
    └── tools
        ├── pom.xml
        └── verification-resources
            ├── pom.xml
            └── src
    

    Here is the output of a reactor build:

    $ mvn compile
    [INFO] Scanning for projects...
    [INFO] Reactor build order: 
    [INFO]   Personal Sandbox - Samples - Parent POM
    [INFO]   Personal Sandbox - Samples - EJB3 and Cargo Sample
    [INFO]   Personal Sandbox - Tools - Parent POM
    [INFO]   Personal Sandbox - Tools - Shared Verification Resources
    [INFO]   Personal Sandbox - Samples - EJB3 and Cargo Sample - Services
    [INFO]   Personal Sandbox - Samples - EJB3 and Cargo Sample - Functests
    [INFO]   Sandbox Externals POM
    ...
    

    This gives IMHO a very decent overview of what is happening, scales correctly, and it's pretty easy to find any module in the file system in case of problems.

    Not sure this does answer all your needs though.

    0 讨论(0)
  • 2020-12-24 13:52

    Here's a way to do this on Linux outside of Maven, by using strace.

    $ strace -o opens.txt -f -e open mvn dependency:tree > /dev/null
    $ perl -lne 'print $1 if /"(.*pom\.xml)"/' opens.txt 
    

    The first line runs mvn dependency:tree under strace, asking strace to output to the file opens.txt all the calls to the open(2) system call, following any forks (because Java is threaded). This file looks something like:

    9690  open("/etc/ld.so.cache", O_RDONLY) = 3
    9690  open("/lib/libncurses.so.5", O_RDONLY) = 3
    9690  open("/lib/libdl.so.2", O_RDONLY) = 3
    

    The second line asks Perl to print any text inside quotes that happens to end in pom.xml. (The -l flag handles printing newlines, the -n wraps the code single quotes in a loop that simply reads any files on the command line, and the -e handles the script itself which uses a regex to find interesting calls to open.)

    It'd be nice to have a maven-native way of doing this :-)

    0 讨论(0)
  • 2020-12-24 13:53

    I had the same problem but solved it without strace. The mvn exec:exec plugin is used to touch pom.xml in every project, and then find the recently modified pom.xml files:

    ctimeref=`mktemp`
    mvn --quiet exec:exec -Dexec.executable=/usr/bin/touch -Dexec.args=pom.xml
    find . -mindepth 2 -type f -name pom.xml -cnewer "$ctimeref" > maven_projects_list.txt
    rm "$ctimeref"
    

    And you have your projects list in the maven_projects_list.txt file.

    0 讨论(0)
  • 2020-12-24 13:58

    This is the command I use for listing all pom.xml files inside a project at the root of the project.

    find -name pom.xml | grep -v target | sort

    What the command do :

    find -name pom.xml what I search

    grep -v target avoid to list pom.xml inside target/ directory

    sort list the result in alphabetical order

    0 讨论(0)
  • 2020-12-24 13:59

    The following script prints artifactId's of all sub-modules:

    mvn -Dexec.executable='echo' -Dexec.args='${project.artifactId}' exec:exec -q

    Example output:

    build-tools
    aws-sdk-java-pom
    core
    annotations
    utils
    http-client-spi
    http-client-tests
    http-clients
    apache-client
    test-utils
    sdk-core
    ...
    
    0 讨论(0)
提交回复
热议问题