Situation with inheritance in maven. I want create hierarchy of projects.
Something like main POM <- module1 <- module2.
I trying in my main POM to define module1 with <module> module1</module>. In module2 i define parent POM with <parent> tag. But after comand "mvn clean package" in main project folder, there is no jar created in the module2 directory.
can someone explain me what i have to do.
Thanks.
You can install the child project into your local repository and include it in the list of dependencies in your parent project. If you're not making changes often to the child project then I've found the install+refresh is quicker overall than having your IDE constantly scanning multiple projects to resolve dependencies.
module1 must have POM packaging
And there you have your answer. Only modules with POM packaging can contain other modules.
Related
Let's say I have a maven project which has some maven modules inside.
My main module depends on the other modules, so when I compile the main module they should be compiled together.
The question is, how to add these modules as dependencies to the main module?
I know if I have a custom lib that I want to use with maven, let's say a utilities project, I have to compile the jar of the project, do a mvn install:install-file on it to install it on the local repository and then add it to the pom.xml.
Do I have to do this with all my modules and add the dependency to the pom.xml on my main module? Because if it should be done like this, there will be a lot of work to do when changing code on the other modules.
What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?
The question is, how to add these modules as dependencies to the main module?
The same way you add any other dependency to your maven project. By adding group id, artifact id and version to <dependency> element
Do I have to do this with all my modules and add the dependency to the pom.xml on my main module?
If your main module depends on some module A then only the pom of the main module should contain dependency declaration towards module A. You do that for all the dependencies of your module.
I don't know what you mean by "a lot of work when changing the code on other modules". Maven has nothing to do with code changes, it just builds the projects whatever they look like at the given moment...
What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?
Any project that you invoke mvn install on gets built and it's jar copied to local repository. That's all you need to do to get the jar into the repo. This will also put all the dependent jars, if available, into the local repo.
As for best practices for multi module projects:
If your parent project (the one that has modules inside) has <modules> section that lists the modules of your application, and modules are in subdirectories of your parent project, then you simply mvn install (or whatever you want to do) the parent project and that will cause all the modules to be built in order defined by declared dependencies between them. That means that if your main module has dependency on module A, then module A will be built before the main module. This way you can build and install all your modules with one command. On the other hand this approach makes more tight coupling between modules which is not desired in some cases, so it depends on your use case whether it is a good approach or not.
I have 2 Java projects where project2 depends on project1's jar. Bot are Maven projects. At the moment this is what I do to create a jar from project2:
c:\eclipse\workspace\project1> mvn install
c:\eclipse\workspace\project2> mvn package
Could I somehow "include" the mvn install command inside projects2's pom.xml so that only the last command would be enough to create the jar?
You have described a basic use case of Maven Reactor (i.e. Maven's fancy name for multi-project/module build).
In this respect, you'd have your two projects alongside a parent pom (which would be the predecessor of both project1 and project2).
Within a Maven multi-module build, modules are allowed to depend one on another; so in your case you'd specify project1 as a dependency of project2's just like you do with any other dependency. Then, when you run package (on the parent pom) Maven will see the dependency, build project1 first and then use the built jar for project2.
I want to deploy this https://github.com/OhadR/oAuth2-sample
as how to author says.
How do I get the 3 war files?
Which maven goal should I run from command line?
oAuth2-sample is the parent module.
The role of this module is to build the other modules defined within pom.xml
So if you see there are 3 modules defined in it(though 2 are commented out),
<modules>
<!--module>auth-common</module>
<module>common-crypto</module-->
<module>authentication-flows</module>
</modules>
So if you run the maven, it will build all the modules as either by jar,war as defined in their individual pom.xml file.
Maven command that you can use,
maven clean - To clear the class files of the projects
maven compile - To compile the source code of the projects
maven install - To install the project in m2 repository for use as dependency for other modules.
Any maven experts out there? I inherited a huge maven project and am trying to get it to compile. Not getting very far. I go to the highest level pom.xml I can find, located in trunk directory, one level down from the main project. Then I issue command "mvn validate". Get the following error:
[INFO] Scanning for projects...
Downloading: http://repo1.maven.org/maven2/com/mycompany/neto/vsd/vsd-superpom/1.1.0/vsd-superpom-1.1.0.pom
[INFO] Unable to find resource 'com.mycompany.neto.vsd:vsd-superpom:pom:1.1.0' in repository central (http://repo1.maven.org/maven2)
I noticed a vsd-superpom folder at the same level as the main project so I'm guessing the main project needs to point to it somewhere? Looking at the pom.xml I see
<parent>
<groupId>com.mycompany.neto.vsd</groupId>
<artifactId>vsd-superpom</artifactId>
<version>1.1.0</version>
</parent>
Where do I put the vsd-superpom folder so that it will be found? I don't understand why it tries to download it. I don't see anything in pom.xml that tells it to do that.
Apache Maven has a two level strategy to resolve and distribute files, which we call artifacts. The first level is called the local repository, which is the artifact cache on your system, by default located at ${user.home}/.m2/repository. When executing Maven, it'll first look in this local cache for artifacts. If the artifact cannot be found here, Maven will access the remote repositories to find the artifact. Once found it will be stored into the local repository, so it's be available for current and future usage.
see Apache Maven Install Plugin Documentation
So if your super pom is independent of the rest of the project you can simply invoke mvn install from the super pom folder so that it will be placed into your local repository. That will solve your problem.
Usually the top-level project pom defines the project dependencies and it should be enough to invoke mvn verify | compile | ...
If the top-level pom depends on the super pom that you have to install the super pom first (or define a pom that contains the submodules super pom and rest of the project)
Common project structure what I have seen (and used) is:
foo-parent
pom.xml - parent POM for my modules with parent ../pom.xml
foo-module
pom.xml - module POM with parent ../foo-parent/pom.xml
...other modules...
pom.xml - multimodule POM without a parent
Now if I want to build foo-module I need to be in the top-level folder and run:
mvn -pl foo-module -am package
In other words you are always building the multi-module project. However you can specify that you are interested only in some submodules (-pl) and their dependencies (-am).
This is just like this other question I asked, but instead of dependencies, this is about modules. Let me give a scenario. You've got a multi-module project and a continuous integration server that deploys the project. This deploys to a repository that your local settings.xml has an updatePolicy of always. Now imagine you run mvn compile right after the CI deploys the project.
Will you end up compiling against the remote server's modules, or your local modules?
In other words, does the updatePolicy of a repository have any effect on the module tags?
modules tag is an aggregation. It is not treated the same as dependencies. Notice that the value that you specify in dependency and module is different. When you declare a dependency, you specify maven coordinates. Whilst for module, you specify relative directories.
Will you end up compiling against the remote server's modules, or your local modules?
You will always end up compiling against your local module if you run the mvn compile in the aggregator module (NOT in the child module!) and the child module dependency is a sibling dependency. When you mvn compile in an aggregator module, the dependency lookup when it is compiling the child module will be special. It will look from the sibling modules target folder ONLY. I can't find documentation on this, but you can try it out.
Imagine that you have these aggregator pom
...
<modules>
<module>child-a</module>
<module>child-b</module>
</modules>
....
child-b pom is declaring a dependency against child-a. (Make sure you have a real output classes)
Next, when you mvn compile the aggregator pom, child-b will be able to compile even if you have NOT mvn install child-a before. That means, child-a is not even in your local repository. This is possible because maven will look into child-a's target folder for the classes. To confirm this, you can try to run mvn compile clean (yes, clean after compile).
Again, do note that this behavior only applies when you run it from the aggregator pom. If your current working directory is child-b (which is dependent on child-a), it will respect the updatePolicy when it tries to look for child-a.