Should I write repositories in my pom.xml? - maven

I am new to Maven. If I start new project with Maven, should I know any repository URLs for it to work?
For example, this Hibernate tutorial http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html says about how to create a sample project with pom.xml text. But this pom.xml does not contain any repositories.
So, my m2eclipse plugin says, for example Project build error: 'dependencies.dependency.version' for org.hibernate:hibernate-core:jar is missing., for all dependency tag in pom.xml
Is this because of repositories absence?
Where to know repositories URLs? Is there one big repository? Why doesn't it included by default?
UPDATE 1
It is said here, that Maven should use "central" repository by default: http://maven.apache.org/guides/introduction/introduction-to-repositories.html
I have searched there for hibernate-code artifact and found it. So, this artifact IS in central repository. By my maven says dependency not found. Hence it doesn't use it's central repository. Why?

Apparently your Hibernate dependency is missing <version> tag:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.9.Final</version> <!-- this line is missing -->
</dependency>
Note that you don't have to specify version of dependencies previously declared in <dependencyManagement>.
Old answer:
Every build script (not only with Maven) should be reproducible and independent from environment. Standard pom.xml (called super pom), which every pom.xml inherits from, already defines main Maven central repository:
<repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
You don't have to define this repository, and you don't have to define any others if all your dependencies are there. On the other hand if you are using some external repositories, you must add them to pom.xml, so that every developer is always able to build.
The bottom line is: if you can build the project having a completely empty repository, your pom.xml is fine.

It's not advisable to define repositories in POM files as that causes a lot of issues (Maven will search those repositories for ANY artifact even the ones available at Central, poor portability, ...)
Best approach: Setup a repository manager (Artifactory, Nexus) and edit your settings.xml file to use the repo manager as a mirror.
Second best approach: Define the required repositories in your settings.xml file, not in your pom.xml files.
Repositories in poms is a bad idea.

Related

Maven doesn't find an existing remote artifact

I'm trying to use docker-compose-rule to run docker-compose files in junit integration tests.
I use the following dependency in my pom.xml file:
<dependency>
<groupId>com.palantir.docker.compose</groupId>
<artifactId>docker-compose-rule-core</artifactId>
<version>0.32.0</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/com.palantir.docker.compose/docker-compose-rule-junit4 -->
<dependency>
<groupId>com.palantir.docker.compose</groupId>
<artifactId>docker-compose-rule-junit4</artifactId>
<version>0.32.0</version>
</dependency>
but for some reason the artifact is not found by maven (for none of the available versions).
As far as I can say, the artifact is found in the jcenter as shows in bintray.
It also exists on maven repository.
Can someone please tell why can't I use these libraries?
Alternatively, can I reference maven to take the library from a specific url with a specific jar file, for example from github, or from here?
According to mvnrepository, the jar you are looking for is not in the maven default Central repository, but is in the Palantir repository. You can add
<repositories>
<repository>
<id>Palantir</id>
<url>https://dl.bintray.com/palantir/releases/</url>
</repository>
</repositories>
into your pom.xml, and try again.

Spring Asciidoctor Extensions not in correct repo?

Good afternoon guys,
I've been trying to get spring-asciidoctor-extensions to work with my documentation but for some reason maven can't find the dependency.
The error i got after my build attempt:
Failed to execute goal org.asciidoctor:asciidoctor-maven-plugin:1.5.5:process-asciidoc (generate-docs) on project organization: Execution generate-docs of goal org.asciidoctor:asciidoctor-maven-plugin:1.5.5:process-asciidoc failed: Plugin org.asciidoctor:asciidoctor-maven-plugin:1.5.5 or one of its dependencies could not be resolved: Failure to find io.spring.asciidoctor:spring-asciidoctor-extensions:jar:0.1.1.RELEASE in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
I checked thier GitHub and looked around to find out that theres nothing in the maven repo (obvious from error) and in this question found that the repo is in https://repo.spring.io/release. Is there a way to reference a dependency to a repo directly? If not what can i do?
You can add the repo.spring.io repository to your pom.xml.
<repositories>
<repository>
<id>spring-repo</id>
<url>https://repo.spring.io/release</url>
</repository>
</repositories>
With that declaration in place you will be able to resolve this dependency:
<dependency>
<groupId>io.spring.asciidoctor</groupId>
<artifactId>spring-asciidoctor-extensions</artifactId>
<version>0.1.1.RELEASE</version>
</dependency>
More details in the docs.
Note: it is not possible to explicitly associate this dependency with that repository. Maven walks through the repositories in order of their declaration until a given dependency is resolved (or not) however if all of your other dependencies are resolved from Maven Central then the only dependency left to be resolved from repo.spring.io will be spring-asciidoctor-extensions.
I ran into this issue as well. My misconception at first was that I expected the dependencies to be resolved from the repositories section. However turns out that it's rather the pluginRepositories section. Like so:
<pluginRepositories>
<pluginRepository>
<id>spring-repo</id>
<url>https://repo.spring.io/release</url>
</pluginRepository>
</pluginRepositories>

Downloading dependency using a custom repository

My company just created a custom repository. I am trying to download a dependency I added to the repository, but the jar doesn't seem to get added to my .m2 directory.
Maven isn't throwing any errors during the install.
Here is the repository in the POM file:
<repositories>
<repository>
<id>snapshots</id>
<url>http://[domain]/artifactory/libs-snapshot</url>
</repository>
</repositories>
and the dependency:
<dependency>
<groupId>com.adobe.aem.sql</groupId>
<artifactId>mysqlAEM</artifactId>
<version>1.0.0-20170313.175547-1</version>
</dependency>
Have also replaced the timestamp with SNAPSHOT.
The path within the repository is:
http://[domain]/artifactory/libs-snapshot-local/com/adobe/aem/sql/mysqlAEM/
And i know the setup of where the JAR file is a little wonky, but I will fix that once I can get it downloaded to my local maven repository.

Can I resolve one Maven dependency from one repo and another dependency from second repo in same POM?

We have a project with multiple parent/child POMs. All the POMs are pointing to a single repository for resolving all Maven dependencies.
Now I have a need like: In a single POM, one jar has to be downloaded from repo1 and rest 4-5 jars from repo2.
How can you do that?
In a word - yes. Maven's dependency resolution mechanism is completely separate from the repository mechanism. Theoretically, you could have every single jar delivered from its own repository (however ridiculous it may to actually do this).
What I have understand the,
I can setup maven repo in artifactory/nexus
e.g. http://localhost:18081/artifactory/ --> L1
1. create remote(R1) repository in artifactory, which can point URL to outside repository, hosted by artifactory/nexus
e.g http://remotehost:18081/artifactory/remote-repo1
2. create a "virtual" repository(V1) in my artifactory and add remote(R1) in to this V1.
3. Let all my poms points to my local artifactory virtual repository(V1),
e.g.>http://localhost:18081/artifactory/virtual
that way, maven will look
a. local .m2 folder
b. then look for jars in virtual repo of my artifactory
hence virtual will look
b1. all local repo
b2. all remote cache repo
b3. all remote repo --> e.g.http://remotehost:18081/artifactory/remote-repo1
I am experimenting this,once succeed, i will update
EDIT :
This has worked for me, the only hiccup I faced was my ~/.m2/settings.xml
the snapshot was false, and my jar in remote repo is a snapshot jar.
After changing this value to true, now its fetching the jars :)
</profile>
<profile>
<id>virtual-repo</id>
<repositories>
<repository>
<id>central</id>
<url><repo_url></url>
<snapshots>
**<enabled>true</enabled>**
</snapshots>
</repository>
</repositories>

Adding db2 jars to java webapp using maven

I'm trying to add the following db2 jars to my Java web application using Maven...
db2jcc_license_cu.jar
db2jcc_javax.jar
db2jcc.jar
I'm following the instructions posted in this post...
Can I add jars to maven 2 build classpath without installing them?
I want to use the static in-project repository solution. So far I have...
Created a folder in my root directory named lib. Inside this
directory lives the three db2 jars.
Added the following to my pom file...
<repository>
<id>lib</id>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc</artifactId>
<version>3.8.47</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_license_cu</artifactId>
<version>3.8.47</version>
</dependency>
But when I run a maven install I get ...
[WARNING] The POM for com.ibm.db2.jcc:db2jcc:jar:3.8.47 is missing, no dependency information available
[WARNING] The POM for com.ibm.db2.jcc:db2jcc_license_cu:jar:3.8.47 is missing, no dependency information available
I got the version of the Jars by running a...
java com.ibm.db2.jcc.DB2Jcc -version
Have I specified this version info corretly? Can anyone suggest what I am doing wrong?
The problem is that you didn't install the jars properly in your "project-maven-repository" (i.e. in the folder ${project.basedir}/lib)
Maven stores (when you do mvn install) the jar files in a maven repository. A maven repository have precise hierarchical structure. Here is a simplified vision of this structure:
the artifact groupId+artifactId define the first part of folder path (in the repository) where the artifact is stored.
the artifact version is the second part of the folder path
the artifact version is also a suffix to the artifact name
the artifactId is the artifact name
the packaging is the artifact extension (default is jar)
By default maven use a repository located under <USER_HOME>/.m2/repository
The solution you are trying to setup use another location for the repository : ${project.basedir}/lib and even if it is not the default repository location it is still a maven-repository and so maven is expecting to find the usual maven repository hierarchy under this location.
That's why you need to organize your ${project.basedir}/lib folder just like a maven repository. That's explained in this part of the referenced post:
Use Maven to install to project repo
Instead of creating this structure by hand I recommend to use a Maven plugin to install your jars as artifacts. So, to install an artifact to an in-project repository under repo folder execute:
mvn install:install-file -DlocalRepositoryPath=lib -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]
If you'll choose this approach you'll be able to simplify the repository declaration in pom to:
<repository>
<id>repo</id>
<url>file://${project.basedir}/lib</url>
</repository>
So you need to do an mvn install to create the ${project.basedir}/lib hierarchy (you can do it by hand, but it's not recommended and error prone).
I your case, the commands to run will be like this: (assuming you put the jar in your HOME_DIR and run this command in your ${project.basedir})
mvn install:install-file -DlocalRepositoryPath=lib -DcreateChecksum=true -Dpackaging=jar -Dfile=<USER_HOME>/db2jcc_license_cu.jar -DgroupId=com.ibm.db2.jcc -DartifactId=db2jcc_license_cu -Dversion=3.8.47
What are the advantages of the approch you choose :
a developer with no maven setup will have the libraries available inside the project sources, under SCM system.
you can easily reference jars that aren't in a public maven repository without the need of something like artifactory or nexus
The drawbacks :
a quite complex folder structure under ${project.basedir}/lib looking very strange for someone not used to work with maven.
you will store the libraries under SCM (lot's of huge binary files)
Another solution would be to download those jars before hand and put them somewhere relatively to your project (like lib directory). Now just tell maven to use those jars. Here the groupId, artifactdId and version are JFYI since they won't be used to download anything.
The merit of this solution is that you won't have to build a maven repository.
<dependencies>
...
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>licences</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc_license_cu.jar</systemPath>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_javax</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc_javax.jar</systemPath>
</dependency>
</dependencies>
Refer Link (Japanese): Mavenリポジトリで提供されていないサードパーティJarをどうするか
I guess these jars do not have a pom.xml. Hence the warning. If the jars get packaged and the application works, then I guess you do not have a problem.

Resources