[英]How to inherit dependencies from other libraries in maven?
I created a custom my-commons
library with maven. 我用maven创建了一个自定义的
my-commons
库。 The commons pom contains eg the following dependency: 公共pom包含例如以下依赖项:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>de.mydomain</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<!-- workaround for http://jira.codehaus.org/browse/MASSEMBLY-603-->
<maven.build.timestamp>${maven.build.timestamp}</maven.build.timestamp>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
I installed the custom library into my local repository. 我将自定义库安装到我的本地存储库中。 In a different project, I reuse the library.
在另一个项目中,我重用了库。 It resolves correctly, so it exists in the repo:
它正确解析,因此它存在于repo中:
<project ...>
<groupId>de.mydomain</groupId>
<artifactId>my-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>my.domain</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Now in this project I'd like to use a class org.springframework.web.bind.annotation.ResponseBody
. 现在在这个项目中,我想使用类
org.springframework.web.bind.annotation.ResponseBody
。
Problem: the class is not in classpath. 问题:该类不在类路径中。 So maven complains
[ERROR] cannot find symbol
. 所以maven抱怨
[ERROR] cannot find symbol
。 But why?? 但为什么?? I'd expect the dependency being inherited!
我希望依赖继承!
Sidenote: I'm using Intellij IDEA if that matters. 旁注:如果重要的话,我正在使用Intellij IDEA。 The same problem applies to all "inherited" libraries.
同样的问题适用于所有“继承的”库。 The spring-web lib is just an example.
spring-web lib就是一个例子。
Please check if .m2 folder contains the jar you want to use or not. 请检查.m2文件夹是否包含您要使用的jar。 You can use
mvn install
command in commons project to generate the jar. 您可以在commons项目中使用
mvn install
命令来生成jar。 Also you need to add commons project in your project pom that you are running. 您还需要在正在运行的项目pom中添加commons项目。
You need to create the artifact for your commons project, through its pom.xml. 您需要通过其pom.xml为commons项目创建工件。
Like in my below example i created a separate project where i am using the AWS and some other dependency and then final jar, which i am using in other project :- 就像我在下面的例子中一样,我创建了一个单独的项目,我正在使用AWS和其他一些依赖项,然后是最终jar,我在其他项目中使用: -
My AWS-client pom.xml looks like below :- 我的AWS客户端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.kaamkaj</groupId>
<artifactId>aws-clients</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aws-clients</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<aws.sdk.version>1.11.104</aws.sdk.version>
</properties>
<dependencies>
<!--AWS dependency-->
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.mercadopago</groupId>
<artifactId>sdk</artifactId>
<version>0.3.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>mercadopago</id>
<url>https://github.com/mercadopago/sdk-java/raw/master/releases</url>
</repository>
</repositories>
</project>
Also, run mvn clean install
on the commons
pom, in your case. 另外,在你的情况下,在
commons
pom上运行mvn clean install
。
And then in all other project where i need to include the aws-client , i use the artifact-id of above project, like below :- 然后在我需要包含aws-client的所有其他项目中,我使用上面项目的artifact-id,如下所示: -
<dependency>
<groupId>com.kaamkaj</groupId>
<artifactId>aws-clients</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Let me know if you have any questions: 如果您有任何疑问,请与我们联系:
Found it: removing the <maven.build.timestamp>...
<property>
solved the problem. 找到它:删除
<maven.build.timestamp>...
<property>
解决了问题。 I don't know why it prevented the inheritance to work properly, especially as I didn't get any errors. 我不知道为什么它阻止继承正常工作,特别是因为我没有得到任何错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.