How to get a Maven dependency to propagate from parent pom to sub-module? - maven-3

I have a Maven multi-module project. I want to define dependencies in the parent pom , (type pom, not jar)
and have sub-modules have the dependency in scope for the code I write. No matter what I do, I cannot get this to work.
Simply putting the dependency in the parent pom does not seem to work, I have to put it yet again in each sub-module.

You can define the dependency versions in parent POM but they are not auto injected. You have to specify the dependencies of the parent that you need in the current project along with their scope so that when maven 'bundles' your code it knows the dependencies(Compile or Provided) before hand. This also affects the size of your JAR file that will be generated

Related

adding selfmade dependencies to a current maven-checkstyle plugin

I need a to add maven-checkstyle-plugin to my master pom. In order to have a common place for checkstyle.xml for all users to use my master pom, I need to add checkstyle.xml to a separate maven project called in my case build-tools-resources and then add this dependency to my maven-checkstyle-plugin as a dependency. Its not working because I am want this maven resource project to be locate under libs-snapshot-local but somehow maven is looking for at http://artifactory.kmd.dk/artifactory/plugins-snapshot, why ??? I want to decide where to keep my maven projects.
Can anyone explain?
Thx in advance :)
You need to add libs-snapshot-local as a pluginrepository in your settings.xml then it works! :)

Managing dependences in a grails app (BuildConfig.groovy vs. dependencies.txt)

I am still somewhat green to Grails. Seems to me that there are multiple locations where you can put dependent JARs, among them are BuildConfig.groovy and then there is dependencies.txt in the grails folder and then there can also be n number of 'lib' folders.
What is the difference between these? When use one over the other? Why can't Grails have one central place where all dependencies are kept?
The preferred method to manage your dependancies is through BuildConfig.groovy since it's uses repositories (maven for example) for making those resources available.
The reason for grails-app/lib being available for use is because there are cases where resources aren't kept in a repository for one reason or another and you need a way to include the resource directly with the application itself.
When in doubt always use BuildConfig.groovy unless you have a use case where you can't.
Update
The dependencies.txt file is simply a listing of the dependencies used by Grails and is not used to resolve them. You can read more about it in the documentation.
You can find a list of dependencies required by Grails in the
"dependencies.txt" file in the root directory of the unpacked
distribution.

Where should I keep my company's parent POM?

We have a root POM in one of our project which more and more people consume. So we're now considering to extract this into a real parent POM (i.e. without <modules> and specific build commands).
Now the question is where should this new parent POM go? I see two options:
A new project
Into a folder parent/ inside of the framework project.
I looked at Maven and it uses a distinct project for the parent POM (here is the latest release).
But that means the relativePath is most often wrong (I really don't want developers to have to checkout the parent POM with this approach).
What are the differences/advantages/drawbacks of the two approaches?
The simple reason having a separate project for such company pom is based on the release cycle of such a pom cause it's different of all other projects/artifacts within the different projects. A release is necessary to have a released version which means a defined state of the company pom. This release is simply being deployed into your company repository manager and can be consumed by many projects. During the time updates are comming in like plugin updates etc. so you can decide making a new release of your company pom. And the different projects can decide to pick the update by simply changing the version or not.

Multiple classifiers in Maven

Being a Maven newbie, I want to know if its possible to use multiple classifiers at once; in my case it would be for generating different jars in a single run. I use this command to build my project:
mvn -Dclassifier=bootstrap package
Logically I would think that this is possible:
mvn -Dclassifier=bootstrap,api package
I am using Maven 3.0.4
Your project seems like a candidate for refactoring into a couple of what Maven calls "modules". This involves splitting the code into separate projects within a single directory tree, where the topmost level is normally a parent or aggregator POM with <packaging>pom</packaging> and a <modules/> list containing the sub-project directory names.
Then, I'd advise putting the API interfaces/exceptions/whatnot into an api/ subdirectory with its own pom.xml, and putting the bootstrap classes into a bootstrap/ subdirectory with its own pom.xml. The top-level pom.xml would then list the modules like this:
<modules>
<module>api</module>
<module>bootstrap</module>
</module>
Once you've refactored the project, you will probably want to add a dependency from the bootstrap module to the api module, since I'm guessing the bootstrap will depend on interfaces/etc. from the api.
Now, you should be able to go into the top level of the directory structure and simply call:
mvn clean install
This approach is good because it forces you to think about how different use cases are supported in your code, and it makes dependency cycles between classes harder to miss.
If you want an example to follow, have a look at one of my github projects: Aprox.
NOTE: If you have many modules dependent on the api module, you might want to list it in the top-level pom.xml in the <dependencyManagement/> section, so you can leave off the version in submodule dependency declarations (see Introduction to the Dependency Mechanism).
UPDATE: Legacy Considerations
If you can't refactor the codebase for legacy reasons, etc. then you basically have two options:
Construct a series of pom.xml files in an empty multimodule structure, and use the build-helper-maven-plugin along with source includes/excludes to fragment the codebase and allocate the classes to different modules out of a single source tree.
Maybe use a plugin like the assembly plugin to carve up the target/classes directory (${project.build.directory}) and allocate classes to the different jars. In this scenario, each assembly descriptor requires an <id/> and by default this value becomes the classifier for that assembly jar. Under this plan, the "main" jar output will still be the monolithic one created by the Maven build. If you don't want this, you can use a separate execution of the assembly plugin, and in the configuration use <appendAssemblyId>false</appendAssemblyId>. If the output of that assembly is a jar, then it will effectively replace the old output from the jar plugin. If you decide to pursue this approach, you might want to read the assembly plugin documents to get as much exposure to different examples as you can.
Also, I should note that in both cases you would be stuck with manipulating the list of things produced by using a set of profiles in the pom in order to turn on/off different parts of the build. I'd highly recommend making the default, un-qualified build one that produces everything. This makes it more likely for things like the release plugin to catch everything you want to release, and up-rev versions, etc. appropriately.
These solutions are usually what I promote as migration steps when you can't refactor the codebase all at once. They are especially useful when migrating from something like an Ant build that produces multiple jars out of a single source tree.

Use multiple files in Ivy makepom Ant target

My project has multiple mostly independent modules each with their own Ivy files. A couple of the modules are 'top-level' in that nothing depends on them, they just depend on other modules.
I'd like to generate a pom for those modules to use to publish the dependencies for my users to consume.
The makepom task only accepts a single file via the ivyfile attribute afaict. I create a master module which declares that it depends on the top-level modules and provide that to makepom, but it does not transitively include the dependencies of the top level modules, it only lists the top level modules themselves.
I realize I could just provide several pom files, but for my sanity, i'd prefer to keep it to just one.
So I am wondering if you can somehow pass multiple Ivy files to the make pom task, or if there is a way to get it to list all the dependencies when I use a master Ivy file? Or at worst, is there an easy way to merge pom files w/o doing it by hand?
If this is a documentation concern, configure each module to generate a report of it's transitive dependencies, see the ivy report task.
The makepom task only accepts a single argument because just like a Maven project modules only have a single file that declares its dependencies.
Perhaps what you could do is create a parent ivy file which has a dependency on each child module?
As Mark O'Connor pointed out, the <ivy:report> task does an excellent job of documenting all the transitive jars. Which ones are required for runtime, compilation, etc.
Another possibility is to use <ivy:retrieve> to retrieve all the jars used by your Master module into an empty directory. Even if this isn't required by your project itself. The list of required jars would be a simple directory listing.

Resources