Maven - How to exclude dependencies from generated JAR

Deepboy

I have 3 projects that looks like this: enter image description here

The problem is that the common DB code has a converter class, and I am getting the below error while running the Business logic.

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

What I did: I tried to exclude the dependencies while packaging the Utility classes using the below code in 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> 

But when I include the exec jar created into the Business logic, it shows that the utility classes are not found.

WHAT I NEED: How do I fix the 'org.hibernate.AssertionFailure'? Should I exclude the dependencies while creating the jar from utility classes? How do I do it?

Incase it matters, I am using Eclipse.

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

Utility Class:

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

Common DB Logic:

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

DaviM

Have you tried to use <exclusions> element on this project dependencies? Like:

Business Logic Project pom.xml file:

    <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

Since you already have CommonDB dependencies on your UtilityProject pom, remove this CommonDB dependency from your business logic pom. This error is because you have 2 classes (same class) being registered with same name. If you already have CommonDB dependency on UtilityProject and you use this UtilityProject as dependency on Business, you don't need to import it twice.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Maven: How to have jar-with-dependencies exclude "provided" dependencies?

From Dev

How to exclude logs generated by maven dependencies in slf4j?

From Dev

How to exclude logs generated by maven dependencies in slf4j?

From Dev

Exclude META-INF/maven folder from the generated jar file

From Dev

How to exclude a package from a jar (with maven)

From Dev

In an Android Gradle build, how to exclude dependencies from an included jar file?

From Dev

How to Exclude Certain Jar Dependencies while creating jar without using maven?

From Dev

maven: exclude jar from JDK

From Dev

How to exclude dependencies in the POM file generated by the Gradle

From Dev

How to use maven assembly plugin to exclude a package from dependency jar?

From Dev

Maven assembly or shade plugin - How to exclude sources from jar?

From Dev

How to depend on jar generated from peer Maven project?

From Dev

Exclude file from jar as built by jar-with-dependencies

From Dev

How do I exclude java files from a jar in maven-jar-plugin?

From Dev

How to exclude target folder from maven source packages generated with mvn deploy?

From Dev

How to include maven dependencies in a jar file?

From Dev

How to exclude unnecessary unmanaged dependencies from packaging?

From Dev

How to exclude {test} scoped dependencies from build?

From Dev

How can I exclude some packages (within a JAR) from a Maven dependency?

From Dev

Maven dynamically exclude class with same name from different dependencies

From Dev

Exclude certain dependencies from class path in a maven project

From Dev

maven exclude plugin from project dependencies by repository definition

From Dev

How to exclude generated code from coverage statistics

From Dev

exclude WEB-INF from a maven generated war

From Dev

How to package a jar and all dependencies within a new jar with maven

From Dev

Building jar with dependencies maven

From Dev

How to exclude transitive dependencies with scope provided with maven-assembly-plugin?

From Dev

How to set the Play framework local dependencies of a WSDL generated JAR?

From Dev

How to set the Play framework local dependencies of a WSDL generated JAR?

Related Related

  1. 1

    Maven: How to have jar-with-dependencies exclude "provided" dependencies?

  2. 2

    How to exclude logs generated by maven dependencies in slf4j?

  3. 3

    How to exclude logs generated by maven dependencies in slf4j?

  4. 4

    Exclude META-INF/maven folder from the generated jar file

  5. 5

    How to exclude a package from a jar (with maven)

  6. 6

    In an Android Gradle build, how to exclude dependencies from an included jar file?

  7. 7

    How to Exclude Certain Jar Dependencies while creating jar without using maven?

  8. 8

    maven: exclude jar from JDK

  9. 9

    How to exclude dependencies in the POM file generated by the Gradle

  10. 10

    How to use maven assembly plugin to exclude a package from dependency jar?

  11. 11

    Maven assembly or shade plugin - How to exclude sources from jar?

  12. 12

    How to depend on jar generated from peer Maven project?

  13. 13

    Exclude file from jar as built by jar-with-dependencies

  14. 14

    How do I exclude java files from a jar in maven-jar-plugin?

  15. 15

    How to exclude target folder from maven source packages generated with mvn deploy?

  16. 16

    How to include maven dependencies in a jar file?

  17. 17

    How to exclude unnecessary unmanaged dependencies from packaging?

  18. 18

    How to exclude {test} scoped dependencies from build?

  19. 19

    How can I exclude some packages (within a JAR) from a Maven dependency?

  20. 20

    Maven dynamically exclude class with same name from different dependencies

  21. 21

    Exclude certain dependencies from class path in a maven project

  22. 22

    maven exclude plugin from project dependencies by repository definition

  23. 23

    How to exclude generated code from coverage statistics

  24. 24

    exclude WEB-INF from a maven generated war

  25. 25

    How to package a jar and all dependencies within a new jar with maven

  26. 26

    Building jar with dependencies maven

  27. 27

    How to exclude transitive dependencies with scope provided with maven-assembly-plugin?

  28. 28

    How to set the Play framework local dependencies of a WSDL generated JAR?

  29. 29

    How to set the Play framework local dependencies of a WSDL generated JAR?

HotTag

Archive