Home > Mobile >  How to run Maven with a pom.xml in different location than the project sources
How to run Maven with a pom.xml in different location than the project sources

Time:12-22

I need to run Maven with a pom.xml file located in different directory than the rest of the project.

For example:

.  maven-f-sample git:(main) ✗ tree .         
├── etc
│   └── pom.xml
└── src
    └── main
        ├── java
        │   └── org
        │       └── example
        │           └── Main.java
        └── resources

When I run mvn from the root of the project with mvn compile -f etc/pom.xml it expects src directory in etc/src.

I know I can modify source and target directories in pom.xml with:

<build>
    <sourceDirectory>../src</sourceDirectory>
    <directory>../target</directory>
</build>

but since other plugins may rely on other directories than src I don't think this is a reliable solution.

Is there a trick to either make Maven use sources located in the same directory from which the command is executed, or to set the project directory with another option, without modifying the pom.xml?

In case you are wondering "why"?

I realise this is not a typical way to use Maven - I need it specifically for Just that generates custom pom.xml in runtime. Right now it creates it in the project directory as pom.xml.tmp but ideally it would be created in /tmp.

CodePudding user response:

While it does not comply with the standard directory layout, you can try calling maven like this:

mvn verify "-Dbasedir=$PWD" "-Dmaven.multiModuleProjectDirectory=$PWD"

However, this is untested. If it doesn't work, please try to comply to the directory layout instead at any cost.

Please also note that some plugins will still not work with this configuration: they might add other directories with different logic.

That said, the solution is (sadly) only: please fix your project. Maybe you can link the file to the base directory?

CodePudding user response:

Actually, I would just change the source and target directories (by using the command line parameters). I am not aware of a plugin that goes around this.

Of course, you need to test this a lot because you might break assumptions.

  • Related