java - Maven - 如何从生成的 JAR 中排除依赖项

标签 java eclipse maven dependency-management

我有 3 个项目,如下所示: enter image description here

问题是公共(public)数据库代码有一个转换器类,我在运行业务逻辑时收到以下错误。

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

我做了什么: 我尝试在 pom.xml 中使用以下代码打包实用程序类时排除依赖项。

            <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>

更新

由于您的 UtilityProject pom 已具有 CommonDB 依赖项,因此请从业务逻辑 pom 中删除此 CommonDB 依赖项。此错误是因为您有 2 个类(同一类)以相同的名称注册。如果您已经具有对 UtilityProject 的 CommonDB 依赖项,并且使用此 UtilityProject 作为对 Business 的依赖项,则无需将其导入两次。

关于java - Maven - 如何从生成的 JAR 中排除依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52392355/

相关文章:

java - 将类型转换器与 Room 一起使用时出现 StackOverFlow 错误

python - Eclipse PyDev 现在将所有对 Tkinter 的引用显示为错误

java - Tomcat - 单击 Web 服务获取 "The requested resource is not available"

grails - 'mvn grails:run-war'无法部署 war ,java.lang.ClassNotFoundException

java - Maven 将生成的文档/json 文件上传到存储库

java - Java 和 PHP 应用程序中的全文搜索支持

java - Eclipse Java 错误 : This selection cannot be launched and there are no recent launches

eclipse - 使用 Eclipse 时,我应该将工作区添加到源代码管理中吗?

java - 野蝇。 FileNotFoundException(访问被拒绝)

java - 如何使用外部绑定(bind)文件覆盖 JAXB 中的默认名称?