How do I build my Maven pom in Eclipse? - java

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

Related

Easily add libraries to Eclipse Java Oxygen

I know there are post about JAVA libraries in Eclipse and I used a way to add some .jar files (like apache.poi and jsoup) to my project. My question is:
Where do we have to put those libraries and what settings do we have to change, so to make the libraries available for every future project we start ?
Thank you!
The most common tools for bringing dependencies into a Java project are Maven, Gradle, and Ant. I'll focus on Maven, as I estimate it to be the most popular of the three (though I have come to prefer Gradle).
I'm going to assume you have the following prerequisites installed on your machine and each of the developers on your team:
JDK 1.7 or above (in other words, Java)
A Java-focused IDE like IntelliJ or Eclipse
Here's what you need to do:
You and your team members will install Maven
You will create a pom.xml for each new project.
You will specify the dependencies for the project (like jsoup) in your pom.xml
Here's what Maven will do for you:
It will download all the dependencies you specify from an online repository like https://mvnrepository.com/
It will cache the dependencies in a directory in your user home folder called .m2 so that it doesn't need to download dependencies more than once per project
It will resolve the dependencies within each of your dependencies and avoid putting the same dependency on the classpath twice
The minimum pom.xml file you need is as follows:
<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>com.yourcompany</groupId>
<artifactId>your-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
</project>
Copy that into a file called pom.xml in your root project directory. You can add any additional dependencies in the <dependencies> section of the XML file. By convention, all of your classes and code should go into the directory structure src/main/java/.
Any Java IDE you use will know how to open a project from a pom.xml file. In IntelliJ, you select File->New->Project from existing sources, navigate to your project's root directory, select your pom.xml, and click Open. IntelliJ will then open your project as a Maven project, read your pom.xml, and make all of your dependencies indexed and available for code completion and compilation.
Hope this helps

Maven dependency on other Maven projects

I have multiple maven projects, to add dependencies between them I just add it to the POM, for example :
<dependency>
<groupId>com.company</groupId>
<artifactId>XMLManagement</artifactId>
<version>1.0</version>
</dependency>
I am working with eclipse, and everything runs and works great until I try to release a jar.
**Problem1: ** Maven throws an error because it can't find the jars of the other maven projects it depends on in the local repository,
and this means that I have to figure out myself the dependencies between the projects, and go to each and every project in the right order and run "mvn clean install"
This does not seem logical since that is the main purpose of maven, there must be a way to tell maven to do it himself.
**Problem2: ** If project A depends on B that depends on C, and I want to use C in A then in eclipse its enough that I added B in the POM of A and it works, but when I run "mvn clean install" maven throws an error that it is missing dependency of C.
This means I have to add the dependency between A and C, which doesn't make sense because in eclipse I already see it under "Maven Dependencies", so if eclipse recognizes it why mvn clean install doesn't?
Note that I am able to produce the jar I need at the end, but only after a lot of hard work as described above.
I know I can use something like nexus or artifactory, but it's an overkill for me and I want to be able to do it in local repository.
I am looking for the proper way to do it, any suggestions?
What you need is a top-level pom that lists each of the things you want build as modules. In maven jargon, this is known as a reactor build.
Something like:
<groupId>this.that</groupId>
<artifactId>build.root</artifactId>
<name>A name</name>
<packaging>pom</packaging>
<modules>
<module>../a.b</module>
<module>../a.c</module>
Try this, find your file settings.xml configure path repository how:
<localRepository>E:\repositoryMavem</localRepository>
Hability your Eclipse (or IDE in use) for this file settings.xml
After go to path your project has pom.xml
run de command
mvn install:install-file -Dfile=E:\repositoryMavem\XMLManagement-1.0.jar -DgroupId=com.company -DartifactId=XMLManagement -Dversion=1.0 -Dpackaging=jar
if you want to use IDE Eclipse (you can).
if run clean for artifact, you need run install again

How do I write a POM file for a jar for Maven Install?

I have a group of jars, yanfs.jar, yanfs-javadoc.jar, and yanfs-sources.jar. These are third party jars that Maven cannot resolve.
I have plan to use the Maven install command:
mvn install:install-file -Dfile=path -DpomFile=path
to add the jars to my maven repository so that I can use it in my code.
I just can't figure out how to write the pom file for the jar so the at least the sources jar is included along with the standard jar file.
This is what I have so far:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sun</groupId>
<artifactId>yanfs</artifactId>
<version>1.2</version>
<description>Artifactory auto generated POM</description>
</project>
Does anyone else know what else I need to include?
There's just no way you can know what to write here. The POM is a file that is bound to the project you are depending on. If this project did not distribute a POM file with it, you are probably out of luck. This file is supposed to contain all the transitive dependencies of the artifact but also a lot of other things like its licensing. You can't make that up.
You could take a look inside yanfs.jar to see if there's an embedded POM file in it. If there's one, it should be under META-INF/maven/{groupId}/{artifactId} and you don't need to tell the maven-install-plugin about it because it will find it automatically during installation:
If the JAR was built by Apache Maven, it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.
Otherwise, there is really nothing you can do except letting maven-install-plugin generate a POM with the help of the generatePom attribute. Note that the POM you currently have was also generated, probably by your Artifactory server (given its content) when you uploaded it.
Concerning the other artifacts yanfs-javadoc.jar, and yanfs-sources.jar, you can tell the maven-install-plugin about them with the javadoc and sources attributes.
You would have the following command:
mvn install:install-file -Dfile=yanfs.jar -Dsources=yanfs-sources.jar -Djavadoc=yanfs-javadoc.jar
I recommend to not bother creating the pom file, instead just let Maven install it for you and figure out the pom.xml for them.
One key property you want is classifier.
mvn install:install-file -Dfile=yanfs.jar -DartifactId=yanfs -DgroupId=com.sun -Dversion=0.0.1
mvn install:install-file -Dfile=yanfs-javadoc.jar -DartifactId=yanfs -DgroupId=com.sun -Dversion=0.0.1 -Dclassifier=javadoc
mvn install:install-file -Dfile=yanfs-sources.jar -DartifactId=yanfs -DgroupId=com.sun -Dversion=0.0.1 -Dclassifier=sources
Another approach that you can do in one line is to use the sources and javadoc properties:
mvn install:install-file -Dfile=yanfs.jar \
-Djavadoc=yanfs-javadoc.jar \
-Dsources=yanfs-sources.jar \
-DartifactId=yanfs -DgroupId=com.sun -Dversion=0.0.1
The install:install-file page shows all the possible options.
You might want to add a "build" section to your pom and include the maven-source-plugin[1] and maven-jar-plugin[2] with some configuration to your liking.
[1] https://maven.apache.org/plugins/maven-source-plugin/usage.html
[2] https://maven.apache.org/plugins/maven-jar-plugin/usage.html
edit : I misinterpreted your question, I was thinking you were trying to maven-ify these 3rd party dependencies by building them locally from source and building them with maven in order to generate the metadata you need for your project to consume. If these dependencies are open source, that is a potential option for your problem.

Maven dependency not getting corresponding POM file downloaded

I've got a maven project that has a dependency that it gets from a remote Nexus repository. I believe that the dependency was not built with maven, and just uploaded with a barebones POM file. The layout on the server looks fine though, so it was probably deployed with maven.
When maven downloads the dependency to my local repository, it downloads the jar file, but doesn't get the POM. At build time, there's a warning that the POM couldn't be found, and no dependency information available. I'm not actually using any of its code directly (it's actually a transitive dependency), so build completes successfully.
The real problem arises when I try to perform site generation for my project. The part that tries to generate the dependency graph report fails, because it can't find the POM for this dependency to work with.
I can't figure out why I'm not getting the POM downloaded, when the jar file for it gets downloaded just fine.
The POM file for that particular dependency looks like this (you can see why I don't think it's built with maven :))
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.component</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0.1</version>
</project>
You'll notice that the root <project> element doesn't contain any namespace or schema information. Could this be related? Could it be Nexus not recognizing it as a POM? Apart from the possibility of some small syntactical character missing or mistaken, this is my current train of thought...please don't let it influence any ideas you may have! :)
Also, while troubleshooting, I've pasted the contents of the remote POM file into the correct file location in my local .m2 repo. Everything works fine when I do that. This isn't an acceptable fix though, because we will need the build to be done on our CI build servers.
Any help/suggestions greatly appreciated!
Edit:
I've managed to temporarily solve my actual problem, but the strangeness here still exists. I solved the problem by explicitly excluding the thing that depends on this from the dependency that's in my pom (the trouble dep is two steps away at least, and I'm not using anything that uses the thing that pulls it in):
<dependency>
<groupId>com.company.utility</groupId>
<artifactId>shared-utility</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>com.company.common.component</groupId>
<artifactId>thing-that-puls-in-bad-artifact</artifactId>
</exclusion>
</exclusions>
</dependency>
I've created a dummy project to prove it, with the following POM:
<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.company.project</groupId>
<artifactId>my-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.company.component</groupId>
<artifactId>bad-artifact</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</project>
Now in ~/.m2/repository/com/company/compnent/bad-artifact/1.0.1/, I've got:
_remote.repositories
bad-artifact-1.0.1.jar
bad-artifact-1.0.1.jar.sha1
bad-artifact-1.0.1.pom.lastUpdated
With no actual POM file.
If you look inside of _remote.repositories, then this file probably contains information which says "downloading the POM failed last time, don't try it again."
That's one of the things where Maven's policy "don't try to download releases again" gets in your way. Try to delete the folder ~/.m2/repository/com/company/compnent/bad-artifact/1.0.1/ and run Maven again to see the error.
It's quite possible that Maven refuses to use such a broken POM since the root element doesn't have the correct XML namespace. But it's hard to tell without seeing the actual error message.
A way to fix this is to download the JAR and to use mvn install:install-file from the command line to install the dependency locally. Even better, you can use mvn deploy:deploy-file to deploy it to your own Nexus server so all other developers now get a "good" version of the POM.
You should also get in contact with the people running the remote Nexus server so they can fix the issue.
Not related to your actual problem, but with maven (at least, recent version), generated jar contains their pom.xml in the META-INF/maven folder of that jar.
You should try to run maven with -e -X, and move your local repository to force Maven to download all, again.
mv "~/.m2/repository" "~/.m2/repository.old"
mvn -X -e dependency:tree
[edit] it was initially a comment, but it will be too long:
As far as I understand your problem, I think it is an error on Nexus, and not on your machine. Any valid solution would require you to mess with that your company Nexus. If you don't have permissions to do anything with your Company Nexus, you can test it with a local Nexus.
You can also enforce use of that Nexus in your ~/.m2/settings.xml like this:
<mirrors>
<mirror>
<id>nexus-local-central</id>
<mirrorOf>central</mirrorOf>
<url>http://localhost:8081/nexus/content/repositories/central</url>
</mirror>
<mirror>
<id>nexus-local-any</id>
<mirrorOf>external:*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
You should not lose too much time as to why it fails, but focus on making it working.
For that, I think you should write a valid pom.xml for that artifact, and redeploy it on the server using the pomFile option:
mvn deploy:deploy-file -DpomFile=valid-pom.xml -Dfile=foobar.jar -Durl=http://nexus:8081 -DrepositoryId=company-nexus-deploy
Or if you are too lazy (or if this command fail), do it from the Nexus GUI!
PS: the Nexus default admin login/password are admin/admin123, and I think there was also deploy/deploy123 for deployment. Most Nexus that I've seen were not configured to use another login/password.

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.

Categories

Resources