【问题标题】:Maven project creation with local repository and local dependencies使用本地存储库和本地依赖项创建 Maven 项目
【发布时间】:2016-11-13 02:29:55
【问题描述】:

问题:pom.xml 将错误显示为缺少工件。 我已经使用本地存储库创建了项目,并在其中复制了所需的 jar 文件。 我尝试动态创建项目,就像将所需的 jar 名称和版本放在 pom.xml 中一样,但它无法像 poi jar 等那样找到它。

pom.xml `

<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.mavenproject.test</groupId>
  <artifactId>testproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testproject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
<repositories>
      <repository>
         <id>localrepository</id>
         <url>C:/Users/anil/.m2/repository</url>
      </repository>
   </repositories>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
     <dependency>
         <groupId>commons-beanutils</groupId>
         <artifactId>commons-beanutils</artifactId>
      </dependency>
     <dependency>
         <groupId>commons-collections</groupId>
         <artifactId>commons-collections</artifactId>
      </dependency>
      <dependency>
         <groupId>commons-logging</groupId>
         <artifactId>commons-logging</artifactId>
      </dependency>
    <dependency>
         <groupId>commons-digester</groupId>
         <artifactId>commons-digester</artifactId>
      </dependency>
       <dependency>
         <groupId>itext</groupId>
         <artifactId>itext</artifactId>
      </dependency>
       <dependency>
         <groupId>jasperreports</groupId>
         <artifactId>jasperreports</artifactId>
      </dependency>
      <dependency>
         <groupId>poi</groupId>
         <artifactId>poi</artifactId>
      </dependency>
  </dependencies>
</project>

` 本地存储库:本地存储库中存在 Jar 文件 {显示几个 jar 文件} Jarfileinformation present in local repository 有效的 POM:

 <?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mavenproject.test</groupId>
  <artifactId>testproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>testproject</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
      </dependency>
      <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.0</version>
      </dependency>
      <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
      </dependency>
      <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
      </dependency>
      <dependency>
        <groupId>commons-digester</groupId>
        <artifactId>commons-digester</artifactId>
        <version>2.1</version>
      </dependency>
      <dependency>
        <groupId>itext</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.7</version>
      </dependency>
      <dependency>
        <groupId>jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>6.2.2</version>
      </dependency>
      <dependency>
        <groupId>poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.10.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.9.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>commons-digester</groupId>
      <artifactId>commons-digester</artifactId>
      <version>2.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>itext</groupId>
      <artifactId>itext</artifactId>
      <version>2.1.7</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>jasperreports</groupId>
      <artifactId>jasperreports</artifactId>
      <version>6.2.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.10.1</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <id>localrepository</id>
      <url>C:/Users/anil/.m2/repository</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\src\main\java</sourceDirectory>
    <scriptSourceDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\src\test\java</testSourceDirectory>
    <outputDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\target\classes</outputDirectory>
    <testOutputDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\target</directory>
    <finalName>testproject-0.0.1-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>D:\Anil Bhendarkar\Company Projects\Rolls-Royce\Workspace\testproject\target\site</outputDirectory>
  </reporting>
</project>

我不明白这个问题。 我的目标是使用这个库作为依赖项创建 maven 项目。

谢谢

【问题讨论】:

  • 开始使用存储库管理器并将所需的 jar 安装到那里,而不是简单地使用 dep.罐子。

标签: maven project


【解决方案1】:

要将本地 jar 添加为依赖项,有两种方法可以实现。

1) 使用范围 system

<dependency>
    <groupId>poi</groupId>
    <artifactId>poi</artifactId>
    <version>1.0</version> <!-- Dummy Version -->
    <scope>system</scope>
    <systemPath>/pathToJar/poi.jar</systemPath>
</dependency>

但是使用这种方式,一些 maven 构建工具由于范围 system 而失败,例如 maven-assembly-pluginmaven-shade-plugin。为了避免这种使用方式。

2) 通过创建本地存储库,

在您的项目目录中创建目录repo。 运行以下 maven 命令将 jar 安装到本地存储库,

mvn deploy:deploy-file -Durl=file:///path/toyourproject/repo/ -Dfile=/pathToJar/poi.jar -DgroupId=poi -DartifactId=poi -Dpackaging=jar -Dversion=1.0

通过 cmd 在项目文件夹中运行mvn deploy 命令。如果您的机器上没有安装mvn,请使用以下链接。

Install Maven in Windows

Install Maven in Ubuntu/UNIX

并添加本地仓库入口 pom.xml。

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

最后添加你的依赖,

<dependency>
     <groupId>poi</groupId>
     <artifactId>poi</artifactId>
     <version>1.0</version>
</dependency>

此依赖项将使用您在项目文件夹中创建的本地 repo 中的 jar。

【讨论】:

  • 感谢您的建议。
  • 我喜欢选择 2,但是 mvn deploy: 我应该在哪里运行这个命令以及为什么我不能将 jar 目录复制到项目存储库。
  • @AnilBhendarkar 检查您的第一个问题的更新部分,使用此命令,maven 将创建适当的 .pom 文件存储库结构,您可以轻松地将其用作 maven 存储库。
  • 这对我来说非常有用,可以自动设置与非 mavenized 第三方 jar 的依赖关系。谢谢!
【解决方案2】:

顺便说一句,对于 Android 项目,如果您需要将其添加到 repo(例如为了在 CI 中构建您的应用),您需要替换

allprojects {
    repositories {
        google()
        mavenLocal()
    }
}

通过

allprojects {
    repositories {
        google()
        maven {
            url rootProject.projectDir.path + "/common_libs"
        }
    }
}

在您的顶级 build.gradle 文件中

【讨论】:

  • 为我工作了另一个用例“rootProject.projectDir.path”谢谢
猜你喜欢
  • 1970-01-01
  • 2014-09-28
  • 2017-11-29
  • 1970-01-01
  • 2012-02-03
  • 2020-07-13
  • 1970-01-01
  • 2014-03-01
  • 2014-06-18
相关资源
最近更新 更多