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 Java

How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

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 Java

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

From Dev

How to "exclude" dependencies embedded in an uber-jar using Maven 3?

From Dev

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

From

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

From Dev

Maven 3: How to exclude generated sources from Xlint check?

From Dev

jar download with dependencies from maven

From Dev

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

From Java

How to exclude a set of packages from maven build jar?

From Dev

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

From Java

How can I run code locally with dependencies and exclude them when creating a Jar with Maven

From Dev

How to depend on jar generated from peer Maven project?

From Java

How to exclude jars generated by maven war plugin?

From Java

How to exclude a jar from gradle

From Java

How to exclude a jar from gradle

From Java

How To: Eclipse Maven install build jar with dependencies

From Java

How to include maven dependencies in a jar file?

From Java

Maven: Exclude "META-INF/maven" folder from JAR

From Dev

How to avoid maven shade plugin from including transitive dependencies from 'test-jar' types?

From Dev

Maven dynamically exclude class with same name from different dependencies

From Dev

maven exclude plugin from project dependencies by repository definition

From

How to exclude generated code from coverage statistics

From Dev

exclude WEB-INF from a maven generated war

From Java

Maven jar with dependencies?

From Java

Including dependencies in a jar with Maven

From Dev

Building jar with dependencies maven

From Dev

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

Related Related

  1. 1

    How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

  2. 2

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

  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" dependencies embedded in an uber-jar using Maven 3?

  6. 6

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

  7. 7

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

  8. 8

    Maven 3: How to exclude generated sources from Xlint check?

  9. 9

    jar download with dependencies from maven

  10. 10

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

  11. 11

    How to exclude a set of packages from maven build jar?

  12. 12

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

  13. 13

    How can I run code locally with dependencies and exclude them when creating a Jar with Maven

  14. 14

    How to depend on jar generated from peer Maven project?

  15. 15

    How to exclude jars generated by maven war plugin?

  16. 16

    How to exclude a jar from gradle

  17. 17

    How to exclude a jar from gradle

  18. 18

    How To: Eclipse Maven install build jar with dependencies

  19. 19

    How to include maven dependencies in a jar file?

  20. 20

    Maven: Exclude "META-INF/maven" folder from JAR

  21. 21

    How to avoid maven shade plugin from including transitive dependencies from 'test-jar' types?

  22. 22

    Maven dynamically exclude class with same name from different dependencies

  23. 23

    maven exclude plugin from project dependencies by repository definition

  24. 24

    How to exclude generated code from coverage statistics

  25. 25

    exclude WEB-INF from a maven generated war

  26. 26

    Maven jar with dependencies?

  27. 27

    Including dependencies in a jar with Maven

  28. 28

    Building jar with dependencies maven

  29. 29

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

HotTag

Archive