49

How to create maven pom, which will make project buildable, can I include propriatery jars with my project directly without having to take them from repository? anyone did this before ?

EDIT :

I don't want to make it runnable by building assembly with dependencies jar, I want it to be buildable. So anyone having this project is able to build it, even if jars are nowhere to be found at any repository.

jmj
  • 237,923
  • 42
  • 401
  • 438
London
  • 14,986
  • 35
  • 106
  • 147
  • If your libs are updated or extended on occasion see [this answer](https://stackoverflow.com/a/48670812/1744774) to [I want to load all JARs from my libs project folder with maven](https://stackoverflow.com/q/48665906/1744774) for a "pomified" way and such avoiding an additional repo folder and cumbersome cmd lines or `install-file` scripts. – Gerold Broser Feb 07 '18 at 19:44

11 Answers11

50

1 Either you can include that jar in your classpath of application
2 you can install particular jar file in your maven reopos by

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    life.java how can I do that(#1 sounds interesting)? Can I put my propriatery jars in src/main/resources ? How can I reference that in my pom ? – London Dec 20 '10 at 16:03
  • @London for one you there is no POM in scene, you simply will have to put jar in classpath of your project you can do it by writing an ant task that will copy your jar to `WEB-INF/lib` . – jmj Dec 20 '10 at 16:15
  • 7
    This approach is not suitable for a team of developers -- only for a one-man band. – 01es Dec 20 '10 at 21:35
  • @01es Approch one is suitable for team , and for 2 each one has to do it once. +1 to your approch, but I feel for a single jar it would be too much to do with your way – jmj Dec 21 '10 at 06:21
  • 2
    There's an analogous solution that depends on you having a local repository manager like Nexus around. It involves using `mvn deploy:deploy-file` to push the library to the repository where it would then be available to all developers. – Tim Clemons Dec 21 '10 at 20:45
42

Possible solutions is put your dependencies in src/main/resources then in your pom :

<dependency>
groupId ...
artifactId ...
version ...
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>

Note: system dependencies are not copied into resulted jar/war
(see How to include system dependencies in war built using maven)

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
ant
  • 22,634
  • 36
  • 132
  • 182
  • 21
    This solution doesn't work. Here follows error message: `dependencies.dependency.systemPath' for groupId:yourJar:jar should not point at files within the project directory, ${project.basedir}/src/main/resources/yourJar.jar will be unresolvable by dependent projects` – mat_boy Jun 13 '13 at 13:20
  • 3
    +1 Solution works for me, so it is not a general issue. – WestCoastProjects Jan 29 '14 at 22:09
  • 2
    [`system`](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies) is deprecated now. – Gerold Broser Feb 07 '18 at 18:47
25

Create a repository folder under your project. Let's take

${project.basedir}/src/main/resources/repo

Then, install your custom jar to this repo:

mvn install:install-file -Dfile=[FILE_PATH] \
-DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] \ 
-Dpackaging=jar -DlocalRepositoryPath=[REPO_DIR]

Lastly, add the following repo and dependency definitions to the projects pom.xml:

<repositories>
    <repository>
        <id>project-repo</id>
        <url>file://${project.basedir}/src/main/resources/repo</url>
    </repository>
</repositories>

<dependencies>    
    <dependency>
        <groupId>[GROUP]</groupId>
        <artifactId>[ARTIFACT]</artifactId>
        <version>[VERS]</version>
    </dependency>
</dependencies>
mostar
  • 4,723
  • 2
  • 28
  • 45
  • 2
    I used the same technique but with `mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:src/main/resources/repo/ -DrepositoryId=project-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]`. You can [see my answer here](http://stackoverflow.com/a/36602391/535203) – Anthony O. Apr 13 '16 at 14:58
  • 1
    I am trying to do this, but my settings still point to coprorate artifactory and it ignores the local repo – Kalpesh Soni Jun 05 '18 at 17:00
7

You can get it from your local driver as below:

<dependency>
    <groupId>sample</groupId>
    <artifactId>com.sample</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
  • 3
    [`system`](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies) is deprecated now. – Gerold Broser Feb 07 '18 at 19:15
7

Why not run something like Nexus, your own maven repo that you can upload 3rd party proprietary jar files, and also proxy other public repositories, to save on bandwith?

This also has some good reasons to run your own maven repository manager.

Larry Shatzer
  • 3,579
  • 8
  • 29
  • 36
  • 4
    A repo manager like Nexus is justified when you have a team using maven. For just one developer, make no sense to me. – Ither Dec 20 '10 at 16:32
  • 3
    Nexus is so easy to set up, that it does make sense to me. I have my own nexus for my research projects because I am tired of always manually installing jars. – jvdbogae Dec 20 '10 at 16:42
6

The really quick and dirty way is to point to a local file:

<dependency>
      <groupId>sampleGroupId</groupId>  
       <artifactId>sampleArtifactId</artifactId>  
       <version>1.0</version> 
      <scope>system</scope>
      <systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
</dependency>

However this will only live on your machine (obviously), for sharing it usually makes sense to use a proper m2 archive (nexus/artifactory) or if you do not have any of these or don't want to set one up a local maven structured archive and configure a "repository" in your pom:

local:

<repositories>
    <repository>
        <id>my-local-repo</id>
        <url>file://C:/DEV//mymvnrepo</url>
    </repository>
</repositories>

remote:

<repositories>
    <repository>
        <id>my-remote-repo</id>
        <url>http://192.168.0.1/whatever/mavenserver/youwant/repo</url>
    </repository>
</repositories>
fl0w
  • 3,593
  • 30
  • 34
  • [`system`](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies) is deprecated now. – Gerold Broser Feb 07 '18 at 19:16
  • 2
    like I wrote "quick and dirty", so deprecated for a good reason. – fl0w Feb 08 '18 at 12:16
3

A good way to achieve this is to have a Maven mirror server such as Sonatype Nexus. It is free and very easy to setup (Java web app). With Nexus one can have private (team, corporate etc) repository with a capability of deploying third party and internal apps into it, while also registering other Maven repositories as part of the same server. This way the local Maven settings would reference only the one private Nexus server and all the dependencies will be resolved using it.

01es
  • 5,362
  • 1
  • 31
  • 40
2

Create a new folder, let's say local-maven-repo at the root of your Maven project.

Just add a local repo iside your <project> of your pom.xml:

<repositories>
    <repository>
        <id>local-maven-repo</id>
        <url>file:///${project.basedir}/local-maven-repo</url>
    </repository>
</repositories>

Then for each external jar you want to install, go at the root of your project and execute:

mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]

(Copied from my reply on a similar question)

Community
  • 1
  • 1
Anthony O.
  • 22,041
  • 18
  • 107
  • 163
1

You can use the maven-assembly-plugin and create a jar with all dependencies included.

n00begon
  • 3,503
  • 3
  • 29
  • 42
jvdbogae
  • 1,241
  • 9
  • 15
  • 1
    this is not what I'm asking , I don't want it to be runnable, I want it to be buildable and runable. If I sent you my project, you wouldn't be able to build it because you don't have my propriatery libs and the project uses it. However if I sent you assembly jar , you'd be able to run it. – London Dec 20 '10 at 16:01
1

You could either add the jar to your project and mess around with the maven-assembly-plugin, or add the jar to your local repository:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -DgeneratePom=true

Where: <path-to-file>  the path to the file to load
       <group-id>      the group that the file should be registered under
       <artifact-id>   the artifact name for the file
       <version>       the version of the file
       <packaging>     the packaging of the file e.g. jar
David J. Liszewski
  • 10,959
  • 6
  • 44
  • 57
  • 1
    this is not really the answer I'm looking for, Imagine this : you download my project and you can build it, without any interaction from your side appart mvn clean install command – London Dec 20 '10 at 16:04
  • @London Thanks for the clarification. I think I understand now. AFAIK, the folder src/main/resources WILL end up on project classpath, but jars therein will not be enumerated on path. I vaguely recall configuring a project with "internal jars" but haven't found it yet. – David J. Liszewski Dec 20 '10 at 17:01
1

I think that the "right" solution here is to add your proprietary libraries to your own repository. It should be simple: create pom for your library project and publish the compiled version on your repository. Add this repository to the list of repositories for your mail project and run build. I believe it will work for you.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • He specified: "So anyone having this project is able to build it, even if jars are nowhere to be found at any repository." and also "without having to take them from repository". I have no idea why maven people love the idea of public repositories so much :) – sarah.ferguson Dec 09 '15 at 13:14