Giter Site home page Giter Site logo

directory-maven-plugin's Introduction

Directory Plugin for Maven

Disclaimer

This plugin is for use in very specific situations!

If you're not comfortable guaranteeing that a whole multimodule project structure will always be present on disk every time a build is run, then be warned that using this plugin may produce inconsistent results. Inconsistencies like this can cause false positives or false negatives - builds that succeed when they should have failed, or fail when they should have succeeded.

You have been warned.

Overview

The Directory Plugin for Maven is used to discover various project-related paths, such as the execution root directory, the directory for a specific project in the current build session, or the highest project basedir (closed to the filesystem root directory) available in the projects loaded from disk (not resolved from a remote repository). The plugin will then reflect this value to the console, and also inject it into each project's properties using the value of the property plugin parameter.

This plugin is meant to be run as part of the standard build lifecycle, to help establish locations for files in multimodule builds, where the directory structure referenced is stable and will always be intact.

Using

The basic plugin declaration looks like the following:

<build>
  <plugins>
    <plugin>
      <groupId>org.commonjava.maven.plugins</groupId>
      <artifactId>directory-maven-plugin</artifactId>
      <version>0.1</version>
      <executions>
        <execution>
          <id>directories</id>
          <goals>
            <goal>[GOAL NAME]</goal>
          </goals>
          <phase>initialize</phase>
          <configuration>
            <property>myDirectory</property>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.7</version>
      <executions>
        <execution>
          <id>echo</id>
          <phase>initialize</phase>
          <goals>
            <goal>run</goal>
          </goals>
          <configuration>
            <target>
              <echo>Test Configuration Directory: ${myDirectory}/src/test/configs</echo>
            </target>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Note: We're using the antrun plugin here to illustrate the proper way to make use of the discovered directories. They will be useful ONLY IN PLUGIN CONFIGURATIONS, NOT DURING POM INTERPOLATION.

execution-root Goal

This goal's output is roughly equivalent to using the ${session.executionRootDirectory} expression in a plugin parameter value. Using the execution-root goal in place of [GOAL NAME] above, we see the following:

[INFO] --- directory-maven-plugin:0.1-SNAPSHOT:execution-root (directories) @ routem-web-admin ---
[INFO] Execution-Root set to: /Users/jdcasey/workspace/couch-java-looseLeaf/routem
[INFO] 
[INFO] --- maven-antrun-plugin:1.7:run (echo) @ routem-web-admin ---
[INFO] Executing tasks

main:
     [echo] Test Configuration Directory: /Users/jdcasey/workspace/couch-java-looseLeaf/routem/src/test/configs

NOTE: This goal will inject a property that contains the absolute path of the directory in which Maven was invoked. Each project will have the property, and any plugins that execute after the directory:execution-root runs will have access to it.

directory-of Goal

If, instead of the execution root, you need to reference a directory in a specific module within the reactor, but don't want to do endless relative-path calculus, you can use the directory-of goal. For this goal to function properly, you need to specify a project parameter containing groupId and artifactId, like this:

<configuration>
  <property>myDirectory</property>
  <project>
    <groupId>org.commonjava.routem</groupId>
    <artifactId>routem-api</artifactId>
  </project>
</configuration>

Now, when we substitute directory-of for [GOAL NAME] in the usage template above, we get output like this:

[INFO] --- directory-maven-plugin:0.1-SNAPSHOT:directory-of (directories) @ routem-web-admin ---
[INFO] Directory of org.commonjava.routem:routem-api set to: /Users/jdcasey/workspace/couch-java-looseLeaf/routem/api
[INFO] 
[INFO] --- maven-antrun-plugin:1.7:run (echo) @ routem-web-admin ---
[INFO] Executing tasks

main:
     [echo] Test Configuration Directory: /Users/jdcasey/workspace/couch-java-looseLeaf/routem/api/src/test/configs

NOTE: This goal will function similarly to execution-root in terms of injecting a project property for later plugins to use. HOWEVER, if the reference project isn't found in the current build session, this goal will fail the build.

highest-basedir Goal

If you have a multimodule project structure on disk, and you want the flexibility to reference directories in a parent project's directory structure, even while executing only the child build, you can use the highest-basedir goal. This goal will traverse all projects and their parents, going up the inheritance hierarchy until it runs into a POM that was resolved from the repository instead of being built from disk. At this point, it will sort the paths given by ${project.basedir} for each project, and attempt to select the shortest path from the list.

If the multimodule hierarchy uses multiple sibling parents from the disk that are on the same path-depth in the filesystem, the goal will fail the build. If this is the case, you may prefer to use directory-of instead.

If we modify the execution-root example to use the highest-basedir goal, then execute from the web/admin subdirectory of our sample project, we will see the following:

[INFO] --- directory-maven-plugin:0.1-SNAPSHOT:highest-basedir (directories) @ routem-web-admin ---
[INFO] Highest basedir set to: /Users/jdcasey/workspace/couch-java-looseLeaf/routem
[INFO] 
[INFO] --- maven-antrun-plugin:1.7:run (echo) @ routem-web-admin ---
[INFO] Executing tasks

main:
     [echo] Test Configuration Directory: /Users/jdcasey/workspace/couch-java-looseLeaf/routem/src/test/configs

That is, the highest parent basedir attainable using the relativePath element of the projects' parent specifications.

directory-maven-plugin's People

Contributors

crossdot avatar dependabot[bot] avatar jdcasey avatar noctarius avatar qsiebers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

directory-maven-plugin's Issues

maven property are not resolved against property of directory-of goal

Hi, I'm using your plugin to define a directory of a module where are placed some integration tests executed by antrun plugin.

I run in the same reactor also the sonarqube analisys software.
I need to specify a sonar property where is located the the folder where the JUnit tests are placed. If I use a property in the parent pom as like

<sonar.reportPath>distribution/server.test/target/surefire-tests</sonar.reportPath>

each module relocate to its baseDir so it does not work.

So I add your directory-of goal in the parent pom. The above property became

<sonar.reportPath>${test.project}/target/surefire-tests</sonar.reportPath>

I thought it worked but the property is not resolved against the new property created with your plugin.

Maven complains some plugins are not marked threadsafe

[WARNING] *****************************************************************
[WARNING] * Your build is requesting parallel execution, but project *
[WARNING] * contains the following plugin(s) that are not marked as *
[WARNING] * @threadsafe to support parallel building. *
[WARNING] * While this /may/ work fine, please look for plugin updates *
[WARNING] * and/or request plugins be made thread-safe. *
[WARNING] * If reporting an issue, report it against the plugin in *
[WARNING] * question, not against maven-core *
[WARNING] *****************************************************************
[WARNING] The following plugins are not marked @threadsafe in ProjectName:
[WARNING] org.codehaus.mojo:cobertura-maven-plugin:2.7
[WARNING] org.codehaus.mojo:xml-maven-plugin:1.0
[WARNING] *****************************************************************

Plugin validation fails because dependencies are not declared in the provided scope

Hi there,
thank you firstly for this great little helper tool :)

I recently ran Maven Plugin Validation on my projects, and it warned me that this plugin declares dependencies in the compile scope that should be in the provided scope instead. Here's the exact message I got:

[WARNING]  * org.commonjava.maven.plugins:directory-maven-plugin:1.0
[WARNING]   Plugin EXTERNAL issue(s):
[WARNING]    * Plugin should declare Maven artifacts in `provided` scope. If the plugin already declares them in `provided` scope, update the maven-plugin-plugin to latest version. Artifacts found with wrong scope: [org.apache.maven:maven-core:3.8.1, org.apache.maven:maven-plugin-api:3.8.1, org.apache.maven:maven-model:3.8.1]

If I understand this correctly, the best practice would be to declare dependencies like maven-core, maven-plugin-api or maven-model with <scope>provided</scope>.

Maven setup:

Apache Maven 3.9.3 (21122926829f1ead511c958d89bd2f672198ae9f)
Maven home: C:\apache-maven-3.8.6-bin\apache-maven-3.8.6
Java version: 19.0.2, vendor: Eclipse Adoptium, runtime: C:\Program Files\Eclipse Adoptium\jdk-19.0.2.7-hotspot
Default locale: de_DE, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

To run the plugin validation yourself, you need a project that uses your plugin and then run mvn -Dmaven.plugin.validation=verbose package which will run a build and show all plugin validation errors at the end.

This is more of a warning rather than an actual error, so fixing it has a rather low priority, but I wanted to let you know nevertheless.

Cheers :)

Relative Path

Hi,

May you add a relative path option in order to return a relative path from the artifact being built instead of an absolute path ?

For instance with directory-of return a path like this ../../../

Regards,

Thomas

Provide configuration for m2e

Currently, there is no configuration for m2e (maven 2 eclipse) connectors. it leads to have errors in project when importing Maven project using this maven plugin in Eclipse

License

What is the license for the plug-in? It'd be great if there was a license file so that projects with licensing restrictions could use this code. Thanks!

Mark plugin @threadSafe to comply with Maven 3.5.0 parallel execution

When executing a parallel build with a directory-maven-plugin execution, Maven 3.5.0 will output a warning

[WARNING] *****************************************************************
[WARNING] * Your build is requesting parallel execution, but project      *
[WARNING] * contains the following plugin(s) that have goals not marked   *
[WARNING] * as @threadSafe to support parallel building.                  *
[WARNING] * While this /may/ work fine, please look for plugin updates    *
[WARNING] * and/or request plugins be made thread-safe.                   *
[WARNING] * If reporting an issue, report it against the plugin in        *
[WARNING] * question, not against maven-core                              *
[WARNING] *****************************************************************
[WARNING] The following plugins are not marked @threadSafe in module:
[WARNING] org.commonjava.maven.plugins:directory-maven-plugin:0.1
[WARNING] Enable debug to see more precisely which goals are not marked @threadSafe.
[WARNING] *****************************************************************

Can you add this please? Thank you very much.

highest-basedir cannot be found when launched from a module with pom packaging

When executing the plugin from a project with packaging 'pom' I get this error:

[ERROR] Failed to execute goal org.commonjava.maven.plugins:directory-maven-plugin:0.1:highest-basedir (directories) on project demo: Cannot find a single highest directory for this project set. First two candidates d
irectories don't share a common root. -> [Help 1]

Launching from a sub module with packaging 'jar' does work.

directory-maven-plugin not able to set the Highest basedir

Hello

I have installed and setup maven 3.8.2
Downloaded jersey source code from https://github.com/eclipse-ee4j/jersey/tree/3.0.1 and Unzipped it.
D:\workdir\XXX\jersey3.0.1\jersey-3.0.1>mvn clean package -Dmaven.test.skip=true -PcheckstyleSkip

I run into problem where directory-maven-plugin is not able to set the Highest basedir. I'm not sure why that could be the case.
Attached pom.xml: pom.txt

Attached output log with highest-basedir set : output.log

I have posed similar question at jersey, but not sure if they can help in regards to this.

Any help greatly appreciated.

Thanks..

Plugin failure on Windows

The directory-maven-plugin plugin was failing for me on my Windows machine with the error "Cannot find a single highest directory for this project set": . I looked into the plugin code and investigated this further. The failure happened on HighestBasedirGoal.java: 109.

The issue seems to be due my windows environment. If I do:
cd c:\src\git\repo
mvn clean install

the maven plugin fails. If I do

cd C:\src\git\repo (note the case sensitivity of c)
mvn clean install

the maven command succeeds. Could this be due to a bug in the plugin? Instead of startsWith or String's compareTo, using File's compareTo - might have solved this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.