簡體   English   中英

Maven-如何從生成的JAR中排除依賴項

[英]Maven - How to exclude dependencies from generated JAR

我有3個看起來像這樣的項目: 在此處輸入圖片說明

問題在於,常見的DB代碼具有轉換器類,並且在運行業務邏輯時出現以下錯誤。

Caused by: org.hibernate.AssertionFailure: AttributeConverter class [class com.td.sba.iep.jpa.converters.ModeltoDBAttributeConverter] registered multiple times

我做了什么:在使用pom.xml中的以下代碼打包Utility類時,我試圖排除依賴項。

            <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- Fools the plugin into creating the code only jar instead of the runnable one -->
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <classifier>exec</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin> 

但是,當我將創建的exec jar包含到業務邏輯中時,它表明未找到實用程序類。

我需要:如何解決“ org.hibernate.AssertionFailure”? 從實用程序類創建jar時應該排除依賴項嗎? 我該怎么做?

萬一重要,我正在使用Eclipse。

附加pom.xml:BusinessLogic

 <?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"> <modelVersion>4.0.0</modelVersion> <!-- Use maven profiles to control Spring Boot profile. --> <profiles> <profile> <id>local</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <activeProfile>local</activeProfile> </properties> </profile> <profile> <id>dev</id> <properties> <activeProfile>dev</activeProfile> </properties> </profile> </profiles> <groupId>com.usermanagement</groupId> <artifactId>BusinessLogicClass</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Business Logic</name> <description>Business Logic to handle user requests</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <org.mapstruct.version>1.2.0.Final</org.mapstruct.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-bean-validators</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.fasterxml.uuid</groupId> <artifactId>java-uuid-generator</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-jdk8</artifactId> <version>1.2.0.Final</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jdk8</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.0.8.RELEASE</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.javers</groupId> <artifactId>javers-core</artifactId> <version>3.11.4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.usermanagement</groupId> <artifactId>CommonDBLogic</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.usermanagement</groupId> <artifactId>UtilityClasses</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <!-- or higher, depending on your project --> <target>${java.version}</target> <!-- or higher, depending on your project --> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> </annotationProcessorPaths> <compilerArgs> <compilerArg> -Amapstruct.defaultComponentModel=spring </compilerArg> </compilerArgs> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <distributionManagement> </distributionManagement> </project> 

實用程序類別:

 <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <groupId>com.usermanagement</groupId> <artifactId>UtilityClasses</artifactId> <version>0.0.1-SNAPSHOT</version> <name>UtilityClasses</name> <description>Utility Classes used by Business Logic class</description> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>net.objectlab.kit</groupId> <artifactId>datecalc-common</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>net.objectlab.kit</groupId> <artifactId>datecalc-jdk8</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.javers</groupId> <artifactId>javers-core</artifactId> <version>3.11.4</version> </dependency> <dependency> <groupId>com.usermanagement</groupId> <artifactId>CommonDBLogic</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.6</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- Fools the plugin into creating the code only jar instead of the runnable one --> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> 

通用數據庫邏輯:

 <?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.usermanagement</groupId> <artifactId>CommonDBLogic</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>CommonDBLogic</name> <description>Common DB Logic used by Business class</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-envers</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>2.0.2.RELEASE</version> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- Fools the plugin into creating the code only jar instead of the runnable one --> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> <distributionManagement> </distributionManagement> </project> 

您是否嘗試過在此項目依賴項上使用<exclusions>元素? 喜歡:

業務邏輯項目pom.xml文件:

    <dependency>
        <groupId>com.utility.project</groupId>
        <artifactId>UtilityProject</artifactId>
        <version>1.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>group.id.jar.you.want.to.exclude</groupId>
                <artifactId>artifact.id.jar.you.want.to.exclude</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

UPDATE

由於您的UtilityProject pom已經具有CommonDB依賴關系,因此請從業務邏輯pom中刪除此CommonDB依賴關系。 此錯誤是因為您有2個類(相同的類)被注冊了相同的名稱。 如果您已經對UtilityProject擁有CommonDB依賴關系,並且將該UtilityProject用作對Business的依賴關系,則無需將其導入兩次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM