Adding external jar in a maven project - java

I have a maven project (I use the jersey template, since I'll be creating an API) and would like to add an external jar file. I tried to follow this reference here
I'm using Eclipse Mars in Windows environment. I added the following depedency to my pom.xml file
Now, when I try to use the jar file in my application, why is that I can't access it? The classes in the jar file couldn't be resolved.

I sometimes use file system based repos for this kind of thing.
<repositories>
<repository>
<id>local-files</id>
<name>local-files</name>
<url>file://c:\test\filerepo</url>
</repository>
</repositories>
Then you can install files into the file repo...
mvn install:install-file -Dfile=onebusaway-gtfs-1.3.3 -DgroupId=org.onebusaway.gtfs -DartifactId=onebusaway-gtfs-1.3.3 -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=c:\test\filerepo
Just include the above <repoisitory> entry into any pom you want to have access to the file repo. For example:
<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.example</groupId>
<artifactId>some-artifact</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>some-artifact</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>local-files</id>
<name>local-files</name>
<url>file://c:\test\filerepo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.onebusaway.gtfs</groupId>
<artifactId>org-onebusaway-gtfs-1.3.3</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>

Related

Publish maven project to a local directory

I want to publish my maven project to a local directory but not in $HOME/.m2/repository. I have created the following directory structure:
$HOME/myrepo
|- third_party
+- private
The directory $HOME/myrepo/third_party holds, as the name suggests, external artifacts from 3rd parties. I want to publish my own projects into $HOME/myrepo/private. Therefore, I use the following adjusted $HOME/.m2/settings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>maven-private</id>
<username></username>
<password></password>
</server>
</servers>
<mirrors>
<mirror>
<id>maven-local</id>
<url>file:///home/user/myrepo/third_party</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>
The file pom.xml for my test project looks like this:
<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.mycompany</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>maven-private</id>
<name>Internal Release Repository</name>
<url>file:///home/user/myrepo/private</url>
</repository>
</distributionManagement>
</project>
When I run
mvn install
The project compiles successfully and all required files are fetched from $HOME/myrepo/third_party, however, after compilation the project is always installed to $HOME/.m2/repository.
How do I have to configure maven in order to publish my projects into the $HOME/myrepo/private directory?
If you want to put the artifacts in a repository other than local, you are not thinking of installing but deploying them. Installing with maven by definition puts the artifacts in local repository - so unless you intend to overwrite the location of .m2/repository and all its contents, just run
mvn deploy
You can change the location of your local Maven repository, i.e. where artifacts are installed, by editing the Maven settings file ~/.m2/settings.xml. Specifically, you are interested in changing the <localRepository> to directory of your choice. One thing to look out for is that path you specify must be an absolute path according to the Configuring your Local Repository section of the user guide. Please also note that this will affect all artifacts (both your private and 3rd party ones) as Maven only supports a single local repository.
Example:
<settings>
<!-- settings -->
<localRepository>/path/to/local/repo/</localRepository>
<!-- more settings -->
</settings>

Maven - How to use an artifact from local folder? [duplicate]

This question already has answers here:
How to add local jar files to a Maven project?
(35 answers)
Closed 2 years ago.
I have a pre-compiled artifact Framework-0.0.1-SNAPSHOT.jar located in \lib folder. In my maven I try to use and include that as below:
<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.heroic.backend</groupId>
<artifactId>BackendHeroic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>BackendHeroic</name>
<dependencies>
<dependency>
<groupId>com.heroic.utilities</groupId>
<artifactId>Framework</artifactId>
<scope>compile</scope>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>
But look like I cannot import anything in my code from that Framework jar file. Can you please let me know what's wrong with my setup above? Thank you
You can add a local repository:
<repositories>
<repository>
<id>local-libs</id>
<name>Libs Local Repository</name>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
Then you can add a dependency:
<dependencies>
<dependency>
<groupId>my.company</groupId>
<artifactId>Framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
But you need to put your jar file under /lib/my/company/0.0.1-SNAPSHOT/Framework-0.0.1-SNAPSHOT.jar
Maven looks in the Internet for dependencies, it does not inspect lib directory.
If this artifact built with maven in another project, just install it into your local repository cache: mvn install
If you just got this jar file from someone, install it with the following command:
mvn install:install-file -Dfile=Framework-0.0.1-SNAPSHOT.jar -DgroupId=com.heroic.utilities -DartifactId=Framework -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
you can write it into bat or sh script for other people.

POM is missing, no dependency information available, cfg.xml not found

I've tried to connect my application with database as described on this site: https://www.visual-paradigm.com/tutorials/hibernateinnetbeans.jsp
I use MySQL and Maven project. But the rest is done the way it was explained on the website and when I run CreateAutoPartsStore I have such output:
The POM for unknown.binary:orm:jar:SNAPSHOT is missing, no dependency information available
[...]
--- exec-maven-plugin:1.2.1:exec (default-cli) # AutoPartsStore ---
org.hibernate.HibernateException: /ormmapping/AutoPartsStore.cfg.xml not found
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2026)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2007)
at org.orm.PersistentManager.<init>(PersistentManager.java:72)
at autopartstore.AutoPartsStorePersistentManager.<init>(AutoPartsStorePersistentManager.java:31)
at autopartstore.AutoPartsStorePersistentManager.instance(AutoPartsStorePersistentManager.java:41)
at ormsamples.CreateAutoPartsStoreData.main(CreateAutoPartsStoreData.java:65)
This is my pom.xml file:
<?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.mycompany</groupId>
<artifactId>AutoPartsStore</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<repositories>
<repository>
<id>unknown-jars-temp-repo</id>
<name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
<url>file:${project.basedir}/lib</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>orm</artifactId>
<version>SNAPSHOT</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
I found some solutions to the problem concerning copying XML files to another location, but none of them worked for me.
Looks like you need to add hibernate jar to the Maven dependecies list.
This:
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>orm</artifactId>
<version>SNAPSHOT</version>
</dependency>
informs maven that your repository contains the artifact unknown.binary:orm:jar:SNAPSHOT but it seems it was never installed, and obviously is not present in Maven Central to be downloaded. What is it expected to point to?
Note that .jar lying around in folder marked as Maven repository does not make it count as installed. Also, Maven does not understand the notion of "Jar directory" and will not automatically add to classpath every jar from lib directory, if it is not declared as dependency.

connecting eclipse maven to a repository not hosted at maven central

I am trying to set up a maven project in eclipse that downloads resources from a maven repository. How do I set this up?
The description of the repository is at this link. I created a new maven project in eclipse then tried Maven...update project and Maven...download sources, but there is still no content added to the project.
Here is the pom.xml I am using:
<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>some.id.i.made.up</groupId>
<artifactId>EverestTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>EverestTest</name>
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<repositories>
<repository>
<id>marc-te-main</id>
<url>http://te.marc-hi.ca/mvn</url>
</repository>
</repositories>
</project>
How do I change my pom.xml or other configurations so that the relevant elements are downloaded?
Note that this library is not hosted in the maven central repository.
Now that you're pointed at the repository, you have to tell Maven which libraries in that repository your project needs. To do this, include the tag:
<dependencies>
<dependency>
<groupId>org.marc.everest</groupId>
<artifactId>everest-core</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.marc.everest</groupId>
<artifactId>everest-connectors-spring</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
The site you linked to has the list of artifacts you may need:
everest-core - Everest core modules
everest-connectors-spring - Everest Spring Connector
everest-formatters-xml-its1 - Everest XML ITS1 Formatter
everest-formatters-xml-dt-r1 - Everest XML Datatypes R1 Formatter
everest-rmim-{ca|uv}-{version} - RMIM Assemblies
You'll need a "dependency" stanza for each one that you want. Similarly, change the version tag to be the version you need.

Maven Import AAR Classes.jar

I'm using Intellij Idea. I'm trying to import couch-lite into my project using Maven.
My pom file:
<?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>*</groupId>
<artifactId>*</artifactId>
<version>0.1.0</version>
<repositories>
<repository>
<id>com.couchbase.lite</id>
<name>couchbase-lite</name>
<url>http://files.couchbase.com/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.couchbase.lite</groupId>
<artifactId>couchbase-lite-android</artifactId>
<version>1.0.0</version>
<type>aar</type>
</dependency>
</dependencies>
The result:
As you can see, I can't access the classes.jar of the aar library. Does anyone have any idea how I can reference the files in that jar?
I'm now using Gradle to build the project. The build times are much longer, but all the libraries are being loaded correctly.

Categories

Resources