Programing

단일 종속성의 모든 전이 종속성을 제외

lottogame 2020. 5. 6. 20:50
반응형

단일 종속성의 모든 전이 종속성을 제외


Maven2에서 단일 전이 의존성을 제외하려면 다음과 같이해야합니다.

<dependency>
  <groupId>sample.group</groupId>
  <artifactId>sample-artifactB</artifactId>
  <version>1</version>
   <exclusions>
     <exclusion>
       <groupId>sample.group</groupId>
       <artifactId>sample-artifactAB</artifactId>
     </exclusion>
   </exclusions>
</dependency>

이 접근법의 문제점은에 의해 기여되는 모든 전이 의존성에 대해이 작업을 수행해야한다는 것 sample-artifactB입니다.

와일드 카드를 사용하여 모든 전이 종속성을 하나씩 제외하는 방법이 있습니까?


maven2의 경우 설명하는 것을 수행 할 수있는 방법이 없습니다. maven 3에는 있습니다. maven 3을 사용 하는 경우이 질문에 대한 다른 답변을 참조하십시오

maven 2의 경우 <exclusions>가있는 종속성에 대한 사용자 지정 pom을 만드는 것이 좋습니다. 해당 종속성을 사용해야하는 프로젝트의 경우 일반적인 아티팩트 대신 사용자 정의 pom에 대한 종속성을 설정하십시오. 이를 통해 단일 <exclusion>을 사용하여 모든 전이 종속성을 제외 할 수는 없지만 종속성을 한 번만 작성하면 모든 프로젝트에서 불필요하고 긴 제외 목록을 유지할 필요가 없습니다.


나를 위해 일한 것은 (Maven의 최신 기능 일 수 있음) 제외 요소에서 와일드 카드를 수행하는 것입니다.

두 개의 WAR 패키지 모듈에서 참조되는 "앱"모듈이 포함 된 다중 모듈 프로젝트가 있습니다. 이러한 WAR 패키지 모듈 중 하나는 실제로 도메인 클래스 만 필요합니다 (그리고 아직 앱 모듈에서 분리하지 않았습니다). 나는 이것이 효과가 있음을 발견했다.

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>app</artifactId>
    <version>${project.version}</version>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

groupId 및 artifactId의 와일드 카드는 일반적으로이 종속성을 사용하여 모듈에 전파되는 모든 종속성을 제외합니다.


내가 찾은 한 가지는 유용합니다.

프로젝트의 부모 POM 또는 가져 오기 가능한 종속성 관리 POM의 dependencyManagement 섹션에서 제외와의 종속성을 설정하면 제외 (또는 버전)를 반복 할 필요가 없습니다.

예를 들어, 부모 POM에 다음이있는 경우 :

<dependencyManagement>
    <dependencies>
    ...         
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
     ....
  </dependencies>
</dependencyManagement>

그런 다음 프로젝트의 모듈은 종속성을 다음과 같이 간단히 선언 할 수 있습니다.

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>

The in the parent POM will specify both the version and the exclusions. I use this technique for nearly all of our projects and it eliminates a lot of repetition.


Three years ago I recommended using Version 99 Does Not Exist, but now I've figured out a better way, especially since Version 99 is offline:

In your project's parent POM, use maven-enforcer-plugin to fail the build if the unwanted dependency creeps into the build. This can be done using the plugin's banned dependencies rule:

<plugin>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
        <execution>
            <id>only-junit-dep-is-used</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <bannedDependencies>
                        <excludes>
                            <exclude>junit:junit</exclude>
                        </excludes>
                    </bannedDependencies>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

Then when that alerts you about an unwanted dependency, exclude it in the parent POM's <dependencyManagement> section:

<dependency>
    <groupId>org.springframework.batch</groupId>
    <artifactId>spring-batch-test</artifactId>
    <version>2.1.8.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </exclusion>
    </exclusions>
</dependency>

This way the unwanted dependency won't show up accidentally (unlike just an <exclusion> which is easy to forget), it won't be available even during compile time (unlike provided scope), there are no bogus dependencies (unlike Version 99) and it'll work without a custom repository (unlike Version 99). This approach will even work based on the artifact's version, classifiers, scope or a whole groupId - see the documentation for details.


I use the following workaround : instead of trying to exclude the artifact in all appropriate dependencies, I draw the dependency as "provided" at top level. For example, to avoid shipping xml-apis "whatever version" :

    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>[1.0,]</version>
        <scope>provided</scope>
    </dependency>

Currently, there's no way to exclude more than one transitive dependency at a time, but there is a feature request for this on the Maven JIRA site:

https://issues.apache.org/jira/browse/MNG-2315


There is a workaround for this, if you set the scope of a dependency to runtime, transitive dependencies will be excluded. Though be aware this means you need to add in additional processing if you want to package the runtime dependency.

To include the runtime dependency in any packaging, you can use the maven-dependency-plugin's copy goal for a specific artifact.


if you need to exclude all transitive dependencies from a dependency artifact that you are going to include in an assembly, you can specify this in the descriptor for the assembly-plugin:

<assembly>
    <id>myApp</id>
    <formats>
        <format>zip</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <useTransitiveDependencies>false</useTransitiveDependencies>
            <includes><include>*:struts2-spring-plugin:jar:2.1.6</include></includes>
        </dependencySet>
    </dependencySets>
</assembly>

If you develop under Eclipse, you can in the POM Editor (advanced tabs enabled) dependency graph look for the dependency you want to exclude of your project and then:

right click on it -> "Exclude Maven Artifact ..." and Eclipse will make the exclusion for you without the need to find out on which dependency the lib is linked.


What is your reason for excluding all transitive dependencies?

If there is a particular artifact (such as commons-logging) which you need to exclude from every dependency, the Version 99 Does Not Exist approach might help.


Update 2012: Don't use this approach. Use maven-enforcer-plugin and exclusions. Version 99 produces bogus dependencies and the Version 99 repository is offline (there are similar mirrors but you can't rely on them to stay online forever either; it's best to use only Maven Central).


In a simular issue I had the desired dependency declared with scope provided. With this approach the transitive dependencies are fetched but are NOT included in the package phase, which is what you want. I also like this solution in terms of maintenance, because there is no pom, or custom pom as in whaley's solution, needed to maintain; you only need to provide the specific dependency in the container and be done


Use the latest maven in your classpath.. It will remove the duplicate artifacts and keep the latest maven artifact..

참고URL : https://stackoverflow.com/questions/547805/exclude-all-transitive-dependencies-of-a-single-dependency

반응형