我有一个maven模块,它有一些依赖.在某个配置文件中,我想要排除其中一些依赖项(确切地说,所有依赖项都具有某个组ID).但是,它们必须存在于所有其他配置文件中.有没有办法从配置文件的依赖项中指定排除项?
Pas*_*ent 13
据我所知,不,你不能停用依赖关系(你可以排除传递依赖,但这不是你要求的),是的,你目前正在使用POM(手动编辑它)是错误的.
因此,您应该将它们放在配置文件中,而不是删除依赖项:
第三种选择是(不是基于档案的):
您可以在配置文件中设置它们,而不是排除配置文件中的依赖项provided
。这不需要任何过于复杂的配置,并将从最终构建中排除您不想要的依赖项。
在所需的配置文件中,添加一个dependencies
部分,复制要排除的部分的声明并将其范围为provided
.
例如,假设您要排除slf4j-log4j12
:
<profiles>
<!-- Other profiles -->
<profile>
<id>no-slf4j-log4j12</id>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<!-- Other profiles -->
</profiles>
Run Code Online (Sandbox Code Playgroud)
我认为也不可能排除直接依赖关系(至少,这里没有提到任何内容)。
您可以做的最好的事情是将每种情况所需的依赖项封装到不同的配置文件中(如已经建议的那样),但是,您需要创建两个“互斥”配置文件,其中一个“默认情况下处于活动状态”。实现此目的最可靠的方法是使用配置文件激活参数,例如
<profiles>
<profile>
<id>default-profile</id>
<activation>
<property><name>!exclude</name></property>
</activation>
<dependencies>
dependency-A
dependency-B
...
</dependencies>
</profile>
<profile>
<id>exclude-profile</id>
<activation>
<property><name>exclude</name></property>
</activation>
<!-- exclude/replace dependencies here -->
</profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)
然后使用“mvn [goal]”将使用配置文件“default-profile”,但“mvn [goal] -Dexclude”将使用配置文件“exclude-profile”。
请注意,在某些情况下,使用“activeByDefault”代替“默认”配置文件的参数可能会起作用,但也可能会导致意外行为。问题是,只要多模块构建的任何其他模块中没有其他活动配置文件,“activeByDefault”就会使配置文件处于活动状态。
我遇到的一种方法是将依赖项放在一个单独的pom中.然后,您可以<exclusions>
通过配置文件添加部分.
<dependencies>
<dependency>
<groupId>my.company.dependencies</groupId>
<artifactId>my-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
</dependencies>
<profile>
<activation>
<activeByDefault>false</activeByDefault>
<property>
<name>exclude-deps</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>my.company.dependencies</groupId>
<artifactId>my-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>my.company</groupId>
<artifactId>bad-dep-1</artifactId>
</exclusion>
<exclusion>
<groupId>my.company</groupId>
<artifactId>bad-dep-2</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
Run Code Online (Sandbox Code Playgroud)