maven dependency issue with parent-child reference - java

I am using apache maven 3.1.0 (after quite some time) on windows for a project put together by somebody else. I have a parent pom & multiple child pom xml files. The child pom contains the following which is causing an issue when I run mvn clean install:
<parent>
<groupId>com.test.platform</groupId>
<artifactId>test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
The error I get from mvn -e points to a dependency issue due to inability to find the artifact (referring to the 0.0.1-SNAPSHOT). I am not sure if I am missing a step or need to change something with my project/pom xml files. Any useful suggestions would be welcome (I can post more detailed error messages if needed). Thanks!
Update:
I have also tried adding a relative path to the child pom xml file though its still giving me an error.
Directory Structure:
Main Directory -> pom.xml file
Child Directory -> pom.xml file (with parent tag) - I added the relative section to it as well.

Interesting that the Introduction to the POM shows the relativePath as pointing directly to the pom.xml file, whereas the POM reference shows the relativePath pointing to the parent module/project, and not to the pom. Perhaps it supports both, but I can tell you that the project I'm looking at now (which works) has <relativePath>../myParentProject</relativePath>
Perhaps you could try to set the relative path in your project to ../
Of course, this all assumes that the answer to the question of whether 0.0.1-SNAPSHOT of the parent actually exists in your repository is YES.

Related

Update version of Maven modules in one place only

I have a Maven project with 2 modules. I want the modules to inherit the version of that project, without defining it as a parent in the POM file of any submodules (the reason behind that is that the modules already have parents). What would be the best way to achieve that?
Importing the version from a properties file doesn't work because maven expects a constant value as a project version, not an expression. Maven plugins such as the version maven plugin or the maven release plugin are not solutions to my problem because I need something that would work in an IDE (I have to use Eclipse for packaging the projects, not my call).
Edit
To clarify things (apologies if my original post was not clear enough)
Main Project POM file
...
<groupId>org.mygroup</groupId>
<artifactId>parentproject</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
...
Module POM file example
...
<groupId>org.mygroup</groupId>
<artifactId>module1</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<!-- Some parent that's NOT the main project, e.g. Spring Boot -->
</parent>
...
What I want is a solution that would allow me to set the version only ONCE (e.g. in the main project POM file) and having every module of that project to "inherit" that version.
You should probably set the parent back to your actual parent project. Version numbers between modules can be simultanuously updated using mvn versions:set. If you need the version number for cross-module dependencies, use ${project.version}. If you want to embed another Maven configuration file for its dependencies, consider using a Bill-of-material (BOM): https://stackoverflow.com/a/14876651 . Hope things brings you to your answer!
I really can't think of any Maven facility that fit 100% your necessities. Event if you could set the parentproject as an actual parent on each submodule, you'd need to specify its version in the parent declaration, so...
But I think of a trick to do the job through an authomatism, so that every time the parent version is changed, it will be automatically propagated to each submodule. It can be done like this:
Program a plugin in the parent project that writes the version id on each of its modules' pom files (for example, through an XSL transformation with the xml-maven-plugin).
Then, link this plugin to the package phase, so that every time the parent is build, the versions gets propagated to the submodules.
You only will have to refresh the submodules projects in Eclipse to make Eclipse be aware of the changes.
But if you don't want to refresh manually, there is still another alternative - fully based on Eclipse:
Make an Ant script to perform the copy-version-to-all-module-poms task. And, instead of calling it from a Maven phase, program an Eclipse builder to call it and, within this builder, program also a refresh of the specific modules. So, every time you execute a build of your project, it will copy its version to the submodules and make Eclipse be aware of this change.

How do I build my Maven pom in Eclipse?

I have a project that uses UI4J, and instead of using external jar I decided to go for maven, I am going to distribute it via git, so I guessed that this is a much better approach.
This is my pom.xml:
<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>Kalamaria</groupId>
<artifactId>KalamariaHarverst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenFirst</name>
<dependencies>
<dependency>
<groupId>com.ui4j</groupId>
<artifactId>ui4j</artifactId>
<version>2.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
The problem is that I can't find a way to get this to work. What ever I try I am still getting errors on the import of the library, meaning that the jar of the ui4j is not imported.
I have (among others) tried to do a "Maven bumvn eclipse:eclipseild" with "clean install" as goal
Downloading source and Updating Project from the Maven menu
even tried to do a mvn eclipse:eclipse from the console, but i got this error
The program 'mvn' can be found in the following packages: * maven *
maven2 Try: sudo apt-get install
How does this work, what should I do to import the declared jars?
Removing <type></type> was the correct first step that you need to do.
Now, there's probably still a pom.lastUpdated file in your repository that is wrong, you need to forcibly override it. The easiest thing to do is just delete the entire directory in your .m2 directory, which is located in your OS dependent home directory. On windows, this would be:
C:\Users\<username>\.m2\repository\com\ui4j
On Linux, this is usually in:
/home/<username>/.m2/repository/com/ui4j
Delete that directory, and then do Maven -> Update project, this should fix your problem.
By the way, mvn eclipse:eclipse is almost never the right thing to do, it's much better to use m2eclipse for your eclipse integrations as it works much more seamlessly.
I am not able to reproduce the behavior of the pom type being added automatically when you add the ui4j dependency. However, most of the time the correct dependency <type> is jar, as that is the default. pom dependencies are most often used when a project is simply a pom and nothing else, which is common as the parent pom of an entire application.
In this case (as in most cases), the type you want is jar, so don't specify a type parameter.
To add a bit more background to #durron597 's correct answer:
tag defines what Maven looks for in the repository when it downloads your artifact. Various types exist. Most common and default is "jar" - Maven will look for something packaged as "jar" - a regular *.jar file. Other types include "test-jar", "war" and "pom". Type "pom" means that Maven will look for something packaged as "pom" - basically your dependency's pom.xml file. Most of the artifacts you refer to are packaged as "jar" and do not supply "pom" packaging. See http://maven.apache.org/pom.html#Dependencies, search for "Type".

how to define path to superpom?

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).

Is it possible to have a jar missing in Maven Central Repository?

With reference to this post,
Is it possible that the jar file itself is missing from Maven Central
Repository?
check this link
You will find this
Download() (0 bytes)?
What is this?
Is this possible?
Why?
Could there be any problem while building that jar/dependency?
Yes, there is going to be a problem: the build will fail because the dependency is missing.
As answered by Chandana from the post you referenced:
you need to download the JAR file and manually install it in to your local repo.
e.g. I suppose that this is the jar in question, download it to some directory, create a file jsr173.pom in the same 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">
<groupId>com.bea.xml</groupId>
<artifactId>jsr173-ri</artifactId>
<version>1.0</version>
</project>
and install it to your local repo like this:
mvn install:install-file -Dfile=jsr173-ri-1.0.jar -DpomFile=jsr173.pom
and the build will pass. The file name and pomFile name will be regenerated by maven according to the data from the jsr173.pom file.
Is this possible?
Yes, it is possible that the JAR file is missing from the repo. It can be because it is a parent pom, but if it is pom referencing a JAR file then that dependency effectively becomes unusable meaning that you have to solve it with the above procedure to use it.
Why?
License issues, it is a copyright reference implementation, see here. It looks like this is a relativley common situation, see this answer.

pom.xml Can't resolve parent?

I am a "noob" with maven and I am running a mvn clean install for a open source project I found. I am trying to get it to build a jar file. When I run it the way it "supposedly" is going to work. I get this error...
Non-resolvable parent POM: Could not find artifact
com.gorillalogic.monkeytalk:monkeytalk:pom:1.0.12-SNAPSHOT and 'parent.relativePath'
points at wrong local POM # line 6, column 10 -> [Help 2]
For the pom.xml file # line 6-10 I have...
<parent>
<groupId>com.gorillalogic.monkeytalk</groupId>
<artifactId>monkeytalk</artifactId>
<version>1.0.12-SNAPSHOT</version>
</parent>
Is there a step I am missing? I can give more code if you need it?
Thanks in advance!
You are missing the parent projects pom.
Most likely your folder structure looks like this:
.../moneytalk_Foo/pom.xml
.../moneytalk_Foo/someFolder
but it should look like this:
.../pom.xml
.../moneytalk_Foo/pom.xml
.../moneytalk_Foo/someFolder
Or that parent is a project into itself and you must download it and call mvn install on it yourself.
the parent project maybe not installed yet, try run the parent project "monkeytalk" as Maven install.
Not sure if this is a cure all for everyone seeing this, but for me it was because my maven settings.xml file was not being found. Maven uses this file to locate remote repos for downloading SNAPSHOT files so if it can't find the settings.xml file then parent.relativePath is essentially undefined. Pretty crappy error message for this IMO, it should dump the value of parent.relativePath to give some indication of an undefined var.

Categories

Resources