bsky
bsky

Reputation: 20222

Maven doesn't see dependencies which are JAR files in a subfolder

I have a project where I use Maven as the build tool.

In my pom.xml file I have these dependencies:

<dependency>
  <groupId>com.my.group1</groupId>
  <artifactId>MyArtifact1</artifactId>
  <version>1.0</version>
</dependency>
<dependency>
  <groupId>com.my.group2</groupId>
  <artifactId>MyArtifact2</artifactId>
  <version>1.1</version>
</dependency>
<dependency>
  <groupId>com.my.group3</groupId>
  <artifactId>MyArtifact3</artifactId>
  <version>1.3</version>
</dependency>

Within the folder where I have my pom.xml, I also have a subfolder, \external. In this subfolder, I have the above listed artifacts as jars: MyArtifact1-1.0.jar, MyArtifact2-1.1.jar, MyArtifact3-1.3.jar.

The problem is that when I run mvn install, I get this:

[ERROR] Failed to execute goal on project my-project: Could not resolve dependencies for project com.my.group4:sample-project:jar:20161207.3: The following artifacts could not be resolved: com.my.group1:MyArtifact1:jar:1.0, com.my.group2:MyArtifact2:jar:1.1, com.my.group3:MyArtifact3:jar:1.3: Failure to find com.my.group1:MyArtifact1:jar:1.0 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

So far, I tied running this:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file  -Dfile=./external/MyArtifact1-1.0.jar -DgroupId=com.my.group1 -DartifactId=MyArtifact1 -Dversion=1.0 -Dpackaging=jar

I tried the above for all 3 packages, but still it looks like it didn't have any effect.

Upvotes: 0

Views: 6884

Answers (3)

Jaime
Jaime

Reputation: 6101

You are trying to include in the maven compilation .jar files you downloaded in your computer ?

Remember that Maven will try to get your dependencies from the local repository first (a folder in your computer) and from the global repositories later. If your dependencies (.jar) are not in those repositories, you will get an error message.

Here I give you some ideas/solutions:

  1. If the .jar files resulted from other maven projects

  2. If the .jar files were downloaded from other websites

    • Sometimes you need .jar files that exist in global maven repositories. You may try first to search in these repositories. You can go to sites such as MVN repository to search or browse the existing packages in the repositories. If you find the dependencies you need, you can obtain too the required dependency to include in the pom.xml.

    • If the .jar files does not exist in any repository, you can install the file in your local repository. There is a tutorial in the Heroku's devcenter and another one in Mkyong. You must pick a groupId-artifactId-version for the file, install the file and add the dependency to the pom.xml of the project. Note that, if the .jar was not created using maven, you must provide all the parameters

      mvn install:install-file  -Dfile=path-to-your-artifact-jar \
          -DgroupId=your.groupId \
          -DartifactId=your-artifactId \
          -Dversion=version \
          -Dpackaging=jar \
          -DlocalRepositoryPath=path-to-specific-local-repo
      

      If the .jar was generated with Maven, that file will include their own pom.xml with metadata. If you use Maven 2.5+, you do not need to provide the groupId-artifactId and version.

      mvn install:install-file  -Dfile=path-to-your-artifact-jar \
          -DlocalRepositoryPath=path-to-specific-local-repo
      
    • There is another option. You can use the system scope with a local systemPath. However, I think this solution requires that people keep the library in the same location for all the developers, or requires to include the .jar file in the project/code repository. Using a repository is a better idea.

      <dependency>
          <artifactId>..</artifactId>
          <groupId>..</groupId>
          <scope>system</scope>
          <systemPath>${basedir}/lib/dependency.jar</systemPath>
      </dependency>
      

Upvotes: 2

Puce
Puce

Reputation: 38132

The recommended way is:

  • use a Maven Repository manager such as Nexus
  • configure Maven to use a Single Repository Group
  • configure proxy repos to other Maven Repositories as needed and add them to the group you specified in your settings file (see previous step)
  • if not already available, configure a repo for 3rd party libs in your repository manager and add it to the group you specified in your settings file (see previous step)
  • if your dependencies are not available from any repo, then add them to your 3rd party repo
  • remove all JARs from the source code

Upvotes: 0

Punit
Punit

Reputation: 344

Please check whether these jars present in your local repository.

You can find it's location from your maven setting.xml file , if not mentioned there then it will take the default path i.e. .m2 folder inside c drive.

You need to check first whether these jars are present there or not. If not, then either you need to copy those in proper folder structure or you can install those jars on your local firing below command :

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

Upvotes: 0

Related Questions