88

I have a JAR in my Android project and I want it to be added to final APK. Okay, here I go:

    <dependency>
        <groupId>com.loopj.android.http</groupId>
        <artifactId>android-async-http</artifactId>
        <version>1.3.2</version>
        <type>jar</type>
        <scope>system</scope>
        <systemPath>${project.basedir}/libs/android-async-http-1.3.2.jar</systemPath>
    </dependency>

But when I am running mvn package I am getting a warning:

[WARNING] Some problems were encountered while building the effective model for **apk:1.0
[WARNING] 'dependencies.dependency.systemPath' for com.loopj.android.http:android-async-http:jar should not point at files within the project directory, ${project.basedir}/libs/android-async-http-1.3.2.jar will be unresolvable by dependent projects @ line 36, column 25

And in the final APK there are no JARs.

How do I fix that?

efpies
  • 3,625
  • 6
  • 34
  • 45
  • 3
    You can't use system scope this way. use install:install-file. – bmargulies Jun 07 '12 at 15:43
  • @bmargulies Can you say what is this scope for? – efpies Jun 07 '12 at 15:55
  • 1
    I switched to gradle and don't have these headaches anymore except now I am trying to use an open source library with maven and temporarily hack a jar in(which is this so easy in gradle and so hard in maven). – Dean Hiller Jan 04 '13 at 19:56
  • 1
    This question has discussion of how to avoid using system scope in Maven: http://stackoverflow.com/questions/3642023/having-a-3rd-party-jar-included-in-maven-shaded-jar-without-adding-it-to-local-r – Mark Butler Feb 19 '13 at 06:12
  • Official documentation about the scope 'system' : http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies – Guillaume Husta May 07 '14 at 13:57
  • Nice solution here: https://gist.github.com/timmolderez/92bea7cc90201cd3273a07cf21d119eb – Majid Alfifi Mar 20 '18 at 03:52
  • v2.5.3 will use the POM inside the jar if is has it, so you can just point mvn to the .jar and it will do the rest. See https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html – ssimm Sep 23 '20 at 14:38
  • even though the question is pretty old, I think it's worth mentioning for those interested, that system scope is deprecated [docs](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#system-dependencies) – orustammanapov Sep 17 '21 at 13:56

9 Answers9

156

I don't know the real reason but Maven pushes developers to install all libraries (custom too) into some maven repositories, so scope:system is not well liked, A simple workaround is to use maven-install-plugin

follow the usage:

write your dependency in this way

<dependency>
    <groupId>com.mylib</groupId>
    <artifactId>mylib-core</artifactId>
    <version>0.0.1</version>
</dependency>

then, add maven-install-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <executions>
        <execution>
            <id>install-external</id>
            <phase>clean</phase>
            <configuration>
                <file>${basedir}/lib/mylib-core-0.0.1.jar</file>
                <repositoryLayout>default</repositoryLayout>
                <groupId>com.mylib</groupId>
                <artifactId>mylib-core</artifactId>
                <version>0.0.1</version>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

pay attention to phase:clean, to install your custom library into your repository, you have to run mvn clean and then mvn install

tremendows
  • 4,262
  • 3
  • 34
  • 51
Ging3r
  • 2,010
  • 2
  • 17
  • 26
  • 11
    Why not use `process-resources` instead of `clean`. The process-resources phase looks more appropriate for such a scenario and it is always called before the compile phase. – jplandrain May 13 '16 at 09:46
  • 1
    in a first installation, are you sure you can pass 'validate' phase which comes before of process-resources in 'built lifecycle' ? ; ), 'clean lifecycle' comes first 'built lifecycle' and it hasn't dependecies with any validation, http://www.tutorialspoint.com/maven/maven_build_life_cycle.htm – Ging3r Aug 24 '16 at 08:19
  • 5
    It works, but how do you install several dependencies? – Renaud Pawlak Feb 20 '17 at 17:06
  • 1
    @RenaudPawlak Use multiple ``? – Frankie Drake Jun 06 '18 at 07:14
  • 6
    Stackoverflow should add a feature that allows community to override OP's choice of correct answer, because IMHO, This answer should be the accepted one! :) – Vijay Chavda Jun 27 '18 at 08:30
  • 2
    As others have commented, binding to `clean` phase is very misleading, it's not part of the default lifecycle, and distorts the meaning of `clean`. Furthermore, the suggested change to use a phase in the default lifecycle (e.g. `validate` or `process-resources`) will fail in a multi-module situation, as dependency resolution is attempted by the aggregator, _prior_ to any custom goals executing for child modules. – wool.in.silver Nov 01 '18 at 14:30
  • can I add a folder instead of a single jar here? – Vicky May 21 '20 at 09:05
  • 1
    in my case neither clean or process-resources worked. my netbeans wouldn't execute the plugin. setting phase to initialize and running mvn initialize, prior to build, only worked.. https://stackoverflow.com/questions/46366787/maven-install-external-jar-through-pom-not-working – hello_earth Dec 18 '20 at 11:49
  • As per many posts this solution is not the recommended one, one it works. Was stuck on this and tried many things, but this worked.. Thanks. – utkarsh Sep 28 '22 at 18:07
25

You will need to add the jar to your local maven repository. Alternatively (better option) specify the proper repository (if one exists) so it can be automatically downloaded by maven

In either case, remove the <systemPath> tag from the dependency

Attila
  • 28,265
  • 3
  • 46
  • 55
19
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>

Try this.

Pang
  • 9,564
  • 146
  • 81
  • 122
user9064925
  • 191
  • 1
  • 2
11

System scope was only designed to deal with 'system' files; files sitting in some fixed location. Files in /usr/lib, or ${java.home} (e.g. tools.jar). It wasn't designed to support miscellaneous .jar files in your project.

The authors intentionally refused to make the pathname expansions work right for that to discourage you. As a result, in the short term you can use install:install-file to install into the local repo, and then some day use a repo manager to share.

BenjaminGolder
  • 1,573
  • 5
  • 19
  • 37
bmargulies
  • 97,814
  • 39
  • 186
  • 310
4

Try this configuration. It worked for me:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warSourceDirectory>mywebRoot</warSourceDirectory>
        <warSourceExcludes>source\**,build\**,dist\**,WEB-INF\lib\*,
            WEB-INF\classes\**,build.*
        </warSourceExcludes>
        <webXml>myproject/source/deploiement/web.xml</webXml>
        <webResources>
            <resource>
                <directory>mywebRoot/WEB-INF/lib</directory>
                <targetPath>WEB-INF/lib</targetPath>
                <includes>
                        <include>mySystemJar1.jar.jar</include>
                         <include>mySystemJar2.jar</include>
                   </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>
BenjaminGolder
  • 1,573
  • 5
  • 19
  • 37
sofiene zaghdoudi
  • 3,108
  • 2
  • 17
  • 11
3

Use a repository manager and install this kind of jars into it. That solves your problems at all and for all computers in your network.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • 3
    We planned to run a repo on local server tomorrow or after tomorrow but before that I should solve this problem some other way. – efpies Jun 07 '12 at 16:09
  • 2
    As @efpies mentioned this may be a pie-in-the-sky answer when a developer does not have permissions/ability to create repo manager. – WestCoastProjects Oct 14 '17 at 03:53
  • These days, if you have docker installed all you need is `docker run -d -p 8081:8081 --name nexus sonatype/nexus3` -- see https://hub.docker.com/r/sonatype/nexus3/ for details. – Thorbjørn Ravn Andersen Jul 23 '19 at 21:28
1

mvn install:install-file -DgroupId=com.paic.maven -DartifactId=tplconfig-maven-plugin -Dversion=1.0 -Dpackaging=jar -Dfile=tplconfig-maven-plugin-1.0.jar -DgeneratePom=true

Install the jar to local repository.

breezily
  • 49
  • 1
0

Thanks to Ging3r i got solution:

follow these steps:

  1. don't use in dependency tag. Use following in dependencies tag in pom.xml file::

    <dependency>
    <groupId>com.netsuite.suitetalk.proxy.v2019_1</groupId>
    <artifactId>suitetalk-axis-proxy-v2019_1</artifactId>
    <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.netsuite.suitetalk.client.v2019_1</groupId>
        <artifactId>suitetalk-client-v2019_1</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.netsuite.suitetalk.client.common</groupId>
        <artifactId>suitetalk-client-common</artifactId>
        <version>1.0.0</version>
    </dependency>
    
  2. use following code in plugins tag in pom.xml file:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <id>suitetalk-proxy</id>
                    <phase>clean</phase>
                    <configuration>
                        <file>${basedir}/lib/suitetalk-axis-proxy-v2019_1-1.0.0.jar</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>com.netsuite.suitetalk.proxy.v2019_1</groupId>
                        <artifactId>suitetalk-axis-proxy-v2019_1</artifactId>
                        <version>1.0.0</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
                <execution>
                    <id>suitetalk-client</id>
                    <phase>clean</phase>
                    <configuration>
                        <file>${basedir}/lib/suitetalk-client-v2019_1-2.0.0.jar</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>com.netsuite.suitetalk.client.v2019_1</groupId>
                        <artifactId>suitetalk-client-v2019_1</artifactId>
                        <version>2.0.0</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
                <execution>
                    <id>suitetalk-client-common</id>
                    <phase>clean</phase>
                    <configuration>
                        <file>${basedir}/lib/suitetalk-client-common-1.0.0.jar</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>com.netsuite.suitetalk.client.common</groupId>
                        <artifactId>suitetalk-client-common</artifactId>
                        <version>1.0.0</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    

I am including 3 jars from lib folder:

including external jar in spring boot project

Finally, use mvn clean and then mvn install or 'mvn clean install' and just run jar file from target folder or the path where install(see mvn install log):

java -jar abc.jar

note: Remember one thing if you are working at jenkins then first use mvn clean and then mvn clean install command work for you because with previous code mvn clean install command store cache for dependency.

ankit
  • 2,591
  • 2
  • 29
  • 54
0

Following this thread I was able to configure the install plugin to load my custom jar, but the plugin was not seeing my configuration when running a mvn install

I'm using the base maven-install-plugin:2.5.2 using the maven:3.6.3-jdk-8 docker image.

I don't fully understand this note in the documentation (at the end of the section), but it seems that you can give the phase goal an execution id forcing it to use your configuration:

Note: Configurations inside the element used to differ from those that are outside in that they could not be used from a direct command line invocation because they were only applied when the lifecycle phase they were bound to was invoked. So you had to move a configuration section outside of the executions section to apply it globally to all invocations of the plugin. Since Maven 3.3.1 this is not the case anymore as you can specify on the command line the execution id for direct plugin goal invocation. Hence if you want to run the above plugin and it's specific execution1's configuration from the command-line, you can execute:

mvn myqyeryplugin:queryMojo@execution1

My final working docker command:

docker run -it --rm --name parser -v "$(shell pwd)":/usr/src/parser -w /usr/src/parser maven:3.6.3-jdk-8 mvn -X install:install-file@install-my-jar-file

Where install-my-jar-file is my executions id <execution><id>install-my-jar-file</id>...

Chris Gregory
  • 99
  • 1
  • 5