将传递依赖性排除在多个级别之外

时间:2015-01-23 10:42:09

标签: java maven

我有这个pom.xml

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>4.1.4.Final</version>
                <exclusions>
                    <exclusion>
                        <groupId>javassist</groupId>
                        <artifactId>javassist</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        <dependency>
            <groupId>ch.simas.qlrm</groupId>
            <artifactId>qlrm</artifactId>
            <version>1.5.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.eclipse.persistence</groupId>
                    <artifactId>javax.persistence</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
</project>

依赖:树是

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-maven 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ gs-maven ---
[INFO] org.springframework:gs-maven:jar:0.1.0
[INFO] +- org.hibernate:hibernate-core:jar:4.1.4.Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.0.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] |  +- org.javassist:javassist:jar:3.15.0-GA:compile
[INFO] |  \- org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile
[INFO] \- ch.simas.qlrm:qlrm:jar:1.5.1:compile
[INFO]    +- org.eclipse.persistence:eclipselink:jar:2.5.1:compile
[INFO]    |  +- org.eclipse.persistence:javax.persistence:jar:2.1.0:compile
[INFO]    |  \- org.eclipse.persistence:commonj.sdo:jar:2.1.1:compile
[INFO]    \- com.h2database:h2:jar:1.3.170:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.142 s
[INFO] Finished at: 2015-01-23T15:43:19+05:30
[INFO] Final Memory: 8M/81M
[INFO] ------------------------------------------------------------------------

即使我已经排除了javax.persistence,我也会在构建时收到以下警告

[WARNING] javax.persistence-2.1.0.jar, hibernate-jpa-2.0-api-1.0.1.Final.jar define 92 overlappping classes:
[WARNING]   - javax.persistence.SecondaryTable
[WARNING]   - javax.persistence.TableGenerator
[WARNING]   - javax.persistence.NamedNativeQueries
[WARNING]   - javax.persistence.TransactionRequiredException
[WARNING]   - javax.persistence.SecondaryTables
[WARNING]   - javax.persistence.JoinTable
[WARNING]   - javax.persistence.Id
[WARNING]   - javax.persistence.Embedded
[WARNING]   - javax.persistence.EntityResult
[WARNING]   - javax.persistence.EntityManager
[WARNING]   - 82 more...
[WARNING] javax.persistence-2.1.0.jar, eclipselink-2.5.1.jar define 24 overlappping classes:
[WARNING]   - javax.persistence.NamedStoredProcedureQuery
[WARNING]   - javax.persistence.ConstructorResult
[WARNING]   - javax.persistence.ParameterMode
[WARNING]   - javax.persistence.Index
[WARNING]   - javax.persistence.AttributeConverter
[WARNING]   - javax.persistence.NamedStoredProcedureQueries
[WARNING]   - javax.persistence.Subgraph
[WARNING]   - javax.persistence.ConstraintMode
[WARNING]   - javax.persistence.Converts
[WARNING]   - javax.persistence.criteria.CriteriaUpdate
[WARNING]   - 14 more...
[WARNING] javax.persistence-2.1.0.jar, eclipselink-2.5.1.jar, hibernate-jpa-2.0-api-1.0.1.Final.jar define 80 overlappping classes:
[WARNING]   - javax.persistence.criteria.SetJoin
[WARNING]   - javax.persistence.criteria.Predicate
[WARNING]   - javax.persistence.CacheRetrieveMode
[WARNING]   - javax.persistence.TupleElement
[WARNING]   - javax.persistence.metamodel.PluralAttribute
[WARNING]   - javax.persistence.AccessType
[WARNING]   - javax.persistence.Access
[WARNING]   - javax.persistence.metamodel.ManagedType
[WARNING]   - javax.persistence.metamodel.ListAttribute
[WARNING]   - javax.persistence.criteria.CriteriaBuilder$Trimspec
[WARNING]   - 70 more...
[WARNING] maven-shade-plugin has detected that some .class files
[WARNING] are present in two or more JARs. When this happens, only
[WARNING] one single version of the class is copied in the uberjar.
[WARNING] Usually this is not harmful and you can skeep these
[WARNING] warnings, otherwise try to manually exclude artifacts
[WARNING] based on mvn dependency:tree -Ddetail=true and the above
[WARNING] output
[WARNING] See http://docs.codehaus.org/display/MAVENUSER/Shade+Plugin
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /Users/sivakr/Documents/java/maven_1/target/gs-maven-0.1.0.jar with /Users/sivakr/Documents/java/maven_1/target/gs-maven-0.1.0-shaded.jar
[INFO] Dependency-reduced POM written at: /Users/sivakr/Documents/java/maven_1/dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.214 s
[INFO] Finished at: 2015-01-23T15:43:11+05:30
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------

如何排除重复的jar?并消除警告

更新依赖关系:树

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-maven 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ gs-maven ---
[INFO] org.springframework:gs-maven:jar:0.1.0
[INFO] +- org.hibernate:hibernate-core:jar:4.1.4.Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.0.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] |  +- org.javassist:javassist:jar:3.15.0-GA:compile
[INFO] |  \- org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile
[INFO] \- ch.simas.qlrm:qlrm:jar:1.5.1:compile
[INFO]    +- org.eclipse.persistence:eclipselink:jar:2.5.1:compile
[INFO]    |  \- org.eclipse.persistence:commonj.sdo:jar:2.1.1:compile
[INFO]    \- com.h2database:h2:jar:1.3.170:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.138 s
[INFO] Finished at: 2015-01-23T18:11:06+05:30
[INFO] Final Memory: 8M/81M
[INFO] ------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:0)

POM更改为排除超过一个级别的依赖项与将一个级别排除在一个级别之外相同。

根据documentation 如果依赖图如下

Project-A
   -> Project-B
        -> Project-D 
              -> Project-E <!-- Exclude this dependency -->
              -> Project-F
   -> Project C

在项目A POM中排除项目E

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>sample.ProjectA</groupId>
  <artifactId>Project-A</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  ...
  <dependencies>
    <dependency>
      <groupId>sample.ProjectB</groupId>
      <artifactId>Project-B</artifactId>
      <version>1.0-SNAPSHOT</version>
      <exclusions>
        <exclusion>
          <groupId>sample.ProjectE</groupId> <!-- Exclude Project-E from Project-B -->
          <artifactId>Project-E</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</project>

答案 1 :(得分:0)

Maven depdendcy plugin输出格式为:

[groupId]:[artifactId]:[type]:[version]

表示依赖:

org.eclipse.persistence:javax.persistence:jar:2.1.0:compile

该组为org.eclipse.persistence,artifactId为javax.persistence

您的排除规则是错误的,组和工件是相反的。它应该是:

<exclusions>
    <exclusion>
        <artifactId>javax.persistence</artifactId>
        <groupId>org.eclipse.persistence</groupId>
    </exclusion>
</exclusions>

编辑:添加maven-shade-plugin配置

要从maven-shade-plugin中排除相关性,请尝试将exclusions添加到插件配置中:

<execution>
    <phase>package</phase>
    <goals>
        <goal>shade</goal>
    </goals>
    <configuration>
        <artifactSet>
            <excludes>
                <exclude>org.eclipse.persistence:javax.persistence</exclude>
            </excludes>
        </artifactSet>
        <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>hello.HelloWorld</mainClass>
            </transformer>
        </transformers>
    </configuration>
</execution>