Override maven scope configuration? - maven

My project has a dependency for which the scope must be set to provided. But when I run the project from my local environment, the scope must be compile. Is there anyway I can define the scope as provided in my pom, but override it when I run from my local environment?

I think the best approach would be using maven profiles, in this way you can leave dependency with proper scope in main dependencies tag and re-declare it whithin your "local" profile.
Then you just have to invoke
mvn clean package -Plocal
to run the build triggering local profile and therefore getting the dependency with the modified scope.
In this way standard build is unaffected and you can run all your build locally with all needed changes as well

Related

How Maven Provided scope interacts with Test scope

My project has a dependency (Provided Scope), then If I run my project under Test Scope for unit tests, then how does Maven find this dependency it needs during unit testing?
Would Maven just use the one in local maven repository (if exists)?
Thank you.
From official documentation:
A dependency with this scope is added to the classpath used for compilation and test, but not the runtime classpath.
So yes, during test phase the dependency will need to be available (or will be downloaded) in your local Maven repo.

Maven dependency resolving in multi module project

I have a question about how the Maven dependency resolving mechanism is working in a multi module project.
Normally I only use 'mvn clean install' when I build my multi module projects and my assumption was that if any module in the project needs a previous module, dependency will be resolved by going local repository and loading the corresponding 'jar'.
For project internal reason, I have to use 'mvn clean compile,' this command naturally does not create any 'jar' while 'install' is not there. So here I started wondering, how the dependency resolution for a multi module project works, while jar' is not created but project still able to see the changes from the previous builds. Does the target directories used for dependency management?
Or for 'mvn clean compile' target directory used but for 'mvn clean install' the local repository.
Can anybody explain me how the dependency resolution works in a 'multi module' project.
Thx for answers.....
I think you will understand better if you look at https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
There is a life cycle in the process of building the jar. The compile target will compile the code and create a complete classes folder in your target directory. This target will resolve all your dependencies in your poms and download any dependencies to your local repo, not already there.
The install target will create the jar from the classes directory and install it in your local repository.
I really think you will need to run the install target to get anything useful.
Maven is made of separate components.
There a component that deal with a given module and among other things try to get its dependencies. It ALWAYS get the dependencies from the local repository, eventually after having downloaded such dependencies. If the dependencies are not there and can't be dowloaded, it will fail. Eventually the module will create its own artifact that it will publish to the local repo.
Then there a compoment that when you ask it to build several maven modules, for example calling mvn at the root of a project does order the various module use the dependencies to find the best ordering for the build so that if a given module depend on another, it will be build after the module it depend on. It then call the previous compoent I described, building each module in order.
In all cases, a given module dependencies are always taken from the local repo. The expectation is that the previous modules that were built before actually pushed their artifact to the local repo typically with the mvn install but you could force it do it at any step thanks to proper configuration (may not be a good idea).
In all case if the previous component jar was not built and put into the repo, there no way such jar can be added in the classpath for the next module to be compiled.
Doing compile only on multiple projets isn't going to be any useful.

Disable Maven Settings inheritance

Maven has concept of global settings and user-level settings, that compose into effective-settings for build purposes, see this.
The question is, how can I disable inheritance of global settings inheritance for some particular build?
Command mvn verify -s settings.xml overrides only user-level settings for me, global ones are still visible in effective-settings.
If you are working on an maven-invoker-plugin test you can define a separate settings.xml file in the src/it/....
If you need having a separate repositories or using particular dependencies you should use the mock repository manager which is intended for such things.
The basic test if you integration tests is correct start with empty local repository by using mvn clean version which should work.
As an example you can take a look at the versions-maven-plugin which uses such setup.

How tell to maven don't bring test dependencies

I have Maven dependencies with scope test.
I add flag Dmaven.test.skip=true but Maven still brings test dependencies.
Is there a way not to bring test dependencies if I want to build only the production part?
This is how maven works - it First tries to check that ako dependencies are available, no matter their scope. Only then it continues to test phase to find out that tests should not be executed.
A possible workaround is to define your test dependencies in a separate test maven profile, which is not applied when you do not want to run tests. Profiles are resolved before any dependence are downloaded, therefore if test dependencies are not added by the profile to the effective pom, they are not downloaded at all.
the flag -Dmaven.test.skip will only skip compilation and execution of your tests within the project you run.
Its often better to use -DskipTests as this will compile the test classes but not run them. See surefire documentation.
This has nothing to do with dependencies. Those are loaded into the classpath depending on their scope and what plugins require. The surefire plugin requires resolution of scope test as it runs the unit tests.
If there are dependencies of scope test which you do not want to use you need to remove them or exclude them if they come in via transitive dependencies (dependencies of dependencies). You can execute a mvn dependency:tree to figure out why jar are in the project.
If you add some dependencies for test scope, maven will first check if the dependency is available or not then it checks the scope.
You can create a maven profile, add test dependencies under the profile and trigger the profile when -Dmaven.test.skip or -Dmaven.test.skip=true option is not present. In this way you can keep your build command unchanged.
You can check this simple project manage-test-dependencies-in-maven-the-proper-way to understand it better.

is it possible to do a Maven build using Build Forge?

Is it possible to do a Maven build using Build Forge? Currently we use Build Forge, ClearCase and ClearQuest with Ant scripts; would like to try a Maven build. Not sure if I can I have no test environment so don't want to mess up any thing and still learning all this stuff too
Maven can be invoked from any build automation framework.
Create a buildforge step that invokes your Maven build as follows:
mvn -s /path/to/maven/settings/files/mysettings.xml clean package
Explicitly selecting the settings file is recommended as this enables you customise the Maven configuration for each project.
Project isolation can be further enhanced by ensuring that each project has it's own local repository location (See the "localRepository" parameter in the settings file documentation)

Resources