How to access files from module using maven without searching it in local .m2 repository & central maven repository? - maven

I have two projects. Project A & B. Project B is dependent on project A.
To run project B, I have to add project A's depedency in project B's pom.xml.
If I change something from Project A. I have to do mvn install again to update local repository with latest code of project A. Then I can run Project B.
Can I access Project A's changed code in project B without doing mvn install?

No. You can't access the updated code. That is because, when you build a maven application, it will get all the dependencies from your .m2 repository. If it does not find the dependency there, then it will search your remote repository.
In your case, when you update A's code and don't build it, then the updated artifact will not be be available in your .m2 repository. Now if you build Project B, then it will try to fetch project A artiufact from .m2 repository. Since you did not build A after modifying the code, B will fetch the artifact that is currently there in the .m2 folder i.e. the artifact with the old code.
Another approach that you can use is to have a parent pom. This is just an aggregator pom which will execute the pom.xml for both of your modules. For eg:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.multispring</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>module-A</module>
<module>module-B</module>
</modules>
</project>
Please be aware that the packaging for this should be pom.

Related

mvn Deployment failed: repository element was not specified in the POM

When I run mvn clean deploy on my project I get an error
Also my project in eclipse displays the following errors which I don't know if they are related to my current problem.
Project configuration not up-to-date with pom.xml
plugin configuration not covered by lifecycle configuration
In addition my eclipse doesn't seems to compile the files correctly. My SpringBoot java files aren't being compiled as java files. I can tell because if I deliberately induce syntax errors, there isn't a compilation error. This is all run on eclipse EE and is part of a maven project so I don't even know if a source folder is needed.
Also I'm displaying my main pom.xml file below and it has compilation errors on "pom" and both "&ndash"
I've tried the following solutions
Eclipse Blue, Maven: Project configuration is not up-to-date with pom.xml
Failed to resolve version for org.apache.maven.archetypes
repository element was not specified in the POM inside distributionManagement element or in -DaltDep loymentRepository=id::layout::url parameter
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fanniemae.dfc</groupId>
<artifactId>dfc_app</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>dfc_angular</module>
<module>dfc_springBoot</module>
</modules>
<!--<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3RELEASE</version>
<relativePath/> <!– lookup parent from repository
–>
</parent>-->
</project>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project dfc_app: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
mvn deploy will deploy the produced artifact to a Maven Repository.
To do so it needs the configuration to which repository it must be deployed and this is missing.
But I assume that you don't want to deploy it to a repository but just build it.
That's mvn install This will install it in your local repository.
Maybe you should start with reading the docs: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Does incrementing depending module version should increment parent module version?

I have parent module A that has module B as its dependency. If I increment version of B for just a simple bug fix, that does not change any API or break anything, do I still need to increment version of A?
Example:
A:1.10.0 --> B:2.5.0
fixing B to 2.5.1
In this case, A on Maven depends on older version of B, therefore, it seems it make sense to increate the version of A, too; so to have:
A:1.10.1 --> B:2.5.1
even though we didnt change anything in A.
Is this correct?
Yes, you are correct. If you change version of any of the dependency, you will need to build and release parent project again. This will create the new released version of your parent module A.
(Here, I am assuming that parent module A packaging type is other than pom. If the packaging type is pom, then its a different story altogether.)
Edited:
When you say parent project, you will declare its packaging as pom. It will have it's own version.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>web-project</module>
</modules>
</project>
You can also refer to some modules (child projects) in your parent pom declaration.
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>web-project</artifactId>
<packaging>war</packaging>
Then for submodules there is no need to declare any version for it. Parent version will be carried over to the child project. You can override version in the child project but that's not a common practice. So your parent pom version will be incremented automatically when you release it with maven. And this new version will be carried over to child projects (modules) also.
See this - Maven project version inheritance - do I have to specify the parent version?
So parent module A, I was referring to the above case.
And maven repo - can be of two types - local repository and remote repository. Local repo is where maven looks for resolviing dependencies. This is on your machine. If maven doesn't find it there, maven retrieve those depedencies from remote repository.

Install remote maven artifact to local repository

I would like to install an artifact from maven central repository to my local repository. Can anyone help me on getting this? With other words, I want some jars from maven central repositories to be downloaded into my local repository, but using maven, not going in browser and downloading needed jar files.
I am not entirely sure why would you want artifacts from maven for reasons other than using them in a maven based project... But since that's what you want:
Maven installs artifacts locally when they are used - that is when you install a project that has them in dependencies. Therefore the simplest solution would be to create a dummy project, put the artifacts you want as dependencies and install it. Something like:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dummy</groupId>
<artifactId>dummy</artifactId>
<version>dummy</version>
<dependencies>
<-- artifacts you want -->
</dependencies>
</project>
within pom.xml file in an empty folder. It will additionally create a dummy artifact in your local repository you might want to get rid of manually if it bothers you.

Intellij navigate through multiple maven projects

So I have a file structure that looks like this:
.git
.project
.classpath
app1
pom.xml
.classpath
.project
src
app2
pom.xml
.classpath
.project
src
app3
pom.xml
.classpath
.project
src
TheAppIWorkOn
pom.xml
.classpath
.project
src
TheAppIWorkOn uses jars from app1, 2, and 3 which are maven dependencies so when I need to edit something in app1, 2, or 3 it's a painful process. If I use the "jump to declaration" functionality it just shows me the locked jar. Is it possible to set it up so that when I "Jump to declaration" it take me to the actual code that I can make edits on?
If you would have a maven reactor build it would have made all the module setup for you. Now you only have dependencies to jar files and IntelliJ cannot know that you have the modules to depend on. But you can help IntelliJ by pointing out the module dependencies and after that you will be able to navigate between all the classes in the project.
The best way would be to create a pom.xml in the root directory of the project. This pom would keep together the different modules and also define the build order. When you want to open the project for the first time you just point IntelliJ to this pom file and all the dependencies would be resolved and setup so the navigation between classes in the different modules is a breeze.
Sample pom file for the root directory:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.stackoverflow</groupId>
<artifactId>Q16589702</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>app1</module>
<module>app2</module>
<module>app3</module>
<module>TheAppIWorkOn</module>
</modules>
</project>
If you cannot do this for some reason then you'll have to inform IntelliJ about the module dependencies. You do this by opening up the Project Structure.
Then select the module TheAppIWorkOn and press the plus sign in the bottom and choose Module Dependency....
And there you can select all modules that you want to have as dependency.
Press Ok and then Ok again.
Now you will be able to navigate between the different classes in the project.
If app1, app2 and app3 also have dependencies between each other then you will have to do the same for them.
But the simplest way is definitely to have a pom file in the root project directory with all inter module dependencies there.

Why Maven is looking for .pom file when .jar is present in the repository?

I have the following dependency in my pom.xml
<dependency>
<groupId>aGroup</groupId>
<artifactId>anArtifact</artifactId>
<version>aVersion</version>
</dependency>
I also have the anArtifact-aVersion.jar file in ~/.m2/repository/aGroup/anArtifact/aVersion directory.
When I start building the project, maven looks for a .pom file instead of using the .jar file and attempts to download the following
http://repo1.maven.org/maven2/aGroup/anArtifact/aVersion/anArtifact-aVersion.pom
How can I configure maven to use the existing .jar file?
Every jar needs to have a pom file describing it, you can just add something simple like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aGroup</groupId>
<artifactId>aArtifactId</artifactId>
<version>aVersion</version>
<packaging>jar</packaging>
<name>a Name</name>
</project>
Run your build using the "-o" switch to use Maven in offline mode. In offline mode, Maven will not check for updates of snapshot dependencies in remote repositories.
the best way to install an artifact to the local repository which were not built by Maven ist to use
mvn install:install-file ...
have a look at the install:install goal.
POM that is installed to nexus will describe the jar. Used to pull the dependencies that are associated to corresponding jar. When we add the jar as dependency to our project, all the jars required for the included jar will be identified through the corresponding pom.
It is looking for the pom to, among other things, resolve the transitive dependencies.

Resources