Add a dependency to a maven module without sources - maven

I have a multi-module Maven project, and some of the modules use Ant for the build.
One such module should depend on another, but the target module doesn't have any Java sources and so it doesn't produce any artifact. Can I add a dependency on such a module, or do I have to produce an empty JAR?

If the only goal is to fix the build order, try adding a dependency on the POM.
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>project-built-by-ant</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>

Related

Maven POM packaging with dependencies

I'm trying to construct a codebase where subsystems can be developed as maven modules, without the importing POM needing to concern itself with the internal structure of the maven module.
The "importing" pom
<project>
<artifactId>application</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<artifactId>submodule-1</artifactId>
</dependency>
</dependencies>
</project>
The "imported" pom
<project>
<artifactId>submodule-1</artifactId>
<packaging>pom</packaging>
<modules>
<module>api</module>
<module>implementation</module>
</modules>
<dependencies>
<dependency>
<artifactId>api</artifactId>
</dependency>
<dependency>
<artifactId>implementation</artifactId>
</dependency>
</dependencies>
</project>
This does seem to work, at least partially; the generated JARs appear to be on the classpath during mvn package. IntelliJ shows the application has a dependency on submodule-1 and transitively on api and implementation. However, mvn dependency:tree fails while building submodule-1 saying
Could not resolve dependencies for project submodule-1:pom:1.0.0-SNAPSHOT: Could not find artifact api:jar:1.0.0-SNAPSHOT
I'm trying to determine if this is a valid pattern, that of having packaging of pom, with defined dependencies which are also defined modules in the POM.
Have I stumbled upon a working-but-not-supported edge case, or is the dependency plugin broken in some way, or I'm breaking it in some way, or something else?

dependency spring-boot-starter-test not inherited in multiple module maven project

In my multi-module maven project, all modules have a dependency of my own "common" module, eg, module "A" have this dependency "common". And the "common" module has a spring-boot-starter-test dependency. However, when I write unit test in this "A" module, it shows that the test dependency not imported. And then I check the dependencies and found that the spring-boot-starter-test is not imported. I want to ask why? In my sense, module "A" ref "common", "common" ref spring-boot-starter-test , so the module "A" should ref spring-boot-starter-test, but the fact is not that. By the way, in spite of this spring-boot-starter-test other dependencies works well via the hirachy. Does anyone know why it is? Thank you in advance.
Most likely in the module "A" dependecy spring-boot-starter-test has scope test. Dependecies with such scope is not transitive. See Dependency Scope section https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html.
The best solution is dependency management. See Dependency Management
Briefly, you need to create parrent module and declare dependency managment sectoin:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
Then inherit your modules from parent and just declare dependency without version and scope
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>A</artifactId>
</dependency>
</dependencies>

Dependencies in Dependency Management vs Dependencies in Maven versions plugin

When I use Maven versions:display-dependency-updates to check dependencies updates, I get two sections of result.
1st:
The following dependencies in Dependency Management have newer
versions:
2nd:
The following dependencies in Dependencies have newer versions:
What's the difference between these two?
The Dependency section of the POM defines the artifacts (jars, zip etc) upon which your project depends. i.e. the artifacts that it requires to compile, run etc.
The Dependency Management section of the POM is used to manage dependency information.
So for example, in the following pom the JUnit dependency is defined completely in the dependencyManagement section of the POM with version=4.11 and scope = test.
In the dependency section you simply need to define the JUnit dependency using the groupId and artifactId and maven automatically picks up the version and scope from the dependencyManagement section.
<?xml version="1.0" encoding="utf-8"?>
<project>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependencies>
</project>
Usually you would define the dependencyManagement section in a parent POM, where you define the version and scope for all dependencies. Then in the child module's you simply need to define the dependencies using the groupId and artifactId. This allows you to centrally manage versions and means you only have to update them in one place.
All of this is far better explained in the maven documentation:
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
The Versions Maven Plugin is simply listing the versions found in each of these sections, as it is possible in the dependencies section to override the version that was defined in the dependencyManagement section.

Maven build parameter to exclude a pom dependency

I have a project which has some dependency:
<dependency>
<groupId>mygroup</groupId>
<artifactId>myartifact</artifactId>
<version>1.0</version>
</dependency>
I would like to have the possibility to include/exclude the dependency optionally in build time, and would be nice to have some parameter in my build, for instance mvn clean package -mygroup or something similar.
I have search in different mvn plugins configuration settings, but I didnĀ“t find it.
Some ideas/inputs?
Thanks

dependencies Needed by a mvn plugin are not resolved

I have created a Maven plugin called foo that needs a JAR file say xyz.jar in order to compile and run. In the pom file of plugin project foo I have provided xyz.jar with provided scope in the dependencies section. I do not want to package the xyz.jar file in the plugin.
The plugin foo will be triggered by another project say bar in its install phase. So I added the plugin foo in the <build><plugins><plugin>...</plugin></plugins></build> section of the project bar. Project bar has the dependency of xyz.jar in its <dependencies> section. When I run install goal the classes present in the xyz.jar and which are referred by the plugin foo throw ClassNotFoundException at runtime.
How can I resolve this?
Every plugin and the Maven Project have separated classloaders, that's why it is not working. In general a plugin shouldn't depend on a dependency of the project. So remove the provided scope will solve it. Manipulating the classloader is very tricky.
I do not want to package the xyz.jar file in the plugin.
Well, it is not. It is just a reference.
I can't tell what your plugin is doing. I suggest to have a look at the plugins from Apache Maven or Codehaus Mojo to see how it is done.
Project and plugin dependencies are separated in Maven. If your plugin requires some external jars you need to provide them in <dependencies> section located in the plugin tag (not together with your project dependencies). For example:
<build>
<plugins>
<plugin>
<groupId>...</groupId>
<artifactId>yourPluginArtifactId</artifactId>
<version>...</version>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>xyz</artifactId>
<version>...</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Resources