Include local jar file in maven project - not find

2020-07-29 02:47发布

问题:

So I'm trying to include two local jar files into a Maven project I have and failing to do so. I've tried working through the solutions in these threads: 1 2 but it still isn't really working. Here are the key pieces of my pom.xml file:

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

and then dependencies:

    <dependency>
        <groupId>edu.mlab.jar1</groupId>
        <artifactId>jar1_local</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>edu.mlab.jar2</groupId>
        <artifactId>jar2_local</artifactId>
        <version>1.0</version>
    </dependency>

Both jar files include package declarations at edu.mlab.jar1 and edu.mlab.jar2 respectively, so that's where I want them. The jar files are in the resources folder, and that's right under the base directory.

That's the setup. Now, when I try mvn package (after mvn clean) I get the following error

[ERROR] Failed to execute goal on project PROJECT: Could not resolve dependencies for project edu.mlab.project:PROJECT:war:1.0-SNAPSHOT: The following artifacts could not be resolved: edu.mlab.jar1:jar1_local:jar:1.0, edu.mlab.jar2:jar2_local:jar:1.0: Failure to find edu.mlab.jar1:jar1_local:jar:1.0 in file:///Users/mlab/Desktop/2016/project_web/resources was cached in the local repository, resolution will not be reattempted until the update interval of local-maven-repo has elapsed or updates are forced -> [Help 1]

I'm not really sure what's going wrong seeing as my jar1 and jar2 are exactly in the resources folder. Also, I've tried the approach of importing them with the system scope, but that won't work for my purposes as I want them included in the war artifact.

Thanks so much!

回答1:

  1. First try to install those two jar files into Maven's local repository using the command below

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

Reference URL: http://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html

Note: your local Maven repository is specified in the installation $M2_HOME\conf\settings.xml e.g.

<localRepository>C:\local_maven_repo</localRepository>
  1. Then use the regular Maven tags

    <dependency>
        <groupId>your.groupId</groupId>
        <artifactId>your-artifactId</artifactId>
        <version>your-version</version>
    </dependency> 
    


回答2:

Assumption: You're using Windows and your jar1 & jar2 is in C:\Users\yourusername folder

  1. Set your M2_HOME environment variable, e.g.

    C:\Users\yourusername>set M2_HOME=C:\apache-maven-3.3.9 C:\Users\yourusername>echo %M2_HOME% C:\apache-maven-3.3.9

  2. Manually create a folder C:\local_maven_repo then edit edit file

    C:\apache-maven-3.3.9\conf\settings.xml as below

  3. run the mvn command to instal jar1 & jar2 into C:\local_maven_repo, e.g.

C:\Users\yourusername>mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=jar1.jar -DgroupId=edu.mlab -DartifactId=jar1 -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=C:\local_maven_repo

C:\Users\yourusername>mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=jar2.jar -DgroupId=edu.mlab -DartifactId=jar2 -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=C:\local_maven_repo

  1. Verify the jar1 & jar2 is in the C:\local_maven_repo, e.g.

C:\Users\yourusername>dir C:\local_maven_repo\edu\mlab\jar1\1.0\

C:\Users\yourusername>dir C:\local_maven_repo\edu\mlab\jar2\1.0\

  1. Open files C:\local_maven_repo\edu\mlab\jar1\1.0\jar1-1.0.pom, and C:\local_maven_repo\edu\mlab\jar2\1.0\jar2-1.0.pom

file: C:\local_maven_repo\edu\mlab\jar1\1.0\jar1-1.0.pom

  <groupId>edu.mlab</groupId>
  <artifactId>jar1</artifactId>
  <version>1.0</version>

file: C:\local_maven_repo\edu\mlab\jar2\1.0\jar2-1.0.pom

  <groupId>edu.mlab</groupId>
  <artifactId>jar2</artifactId>
  <version>1.0</version>

copy the following and include them in your pom.xml as below

<dependency>
  <groupId>edu.mlab</groupId>
  <artifactId>jar1</artifactId>
  <version>1.0</version>
<dependency>
<dependency>
  <groupId>edu.mlab</groupId>
  <artifactId>jar2</artifactId>
  <version>1.0</version>
<dependency>