Excluding .class file from jar maven dependency

I had 3 maven projects A, B, C. A depends on B which in turn depends on C. POM has dependency on B and B POM has dependency on C. I want to exclude class file in C when creating A.

How can i do this? I tried to do it using the maven-jar-plugin exception, but couldn't succeed.

+3


source to share


2 answers


Use maven-jar-plugin



<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
      <execution>
        <id>default-jar</id>
        <phase>package</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        <configuration>
          <excludes>
            <exclude><!-- package you want to exclude --></exclude>
          </excludes>
        </configuration>
      </execution>
    </executions>
</plugin>

      

+2


source


There is no quick and easy way to do this. If you add the JAR as a dependency on your project, the JAR will be added to the test or compile the classpath (based on the scope you provided).

maven-dependency-plugin

provides a target unpack

that can be used to unpack artifacts into a specific directory.

As a work around for your problem, you can unpack the C artifact into A target / classes at build time. The target unpack

provides the ability to exclude certain file patterns (so you can exclude certain classes).

Also make sure the C JAR is not added to the class A path (adjust your dependencies accordingly), maybe add a dependency for the C type pom

instead jar

).



https://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html

How to exclude C-jar from A without changing B

    <dependency>
        <artifactId>B</artifactId>
        <groupId>bgroupId</groupId>
        <version>bverison</version>
        <exclusions>
            <exclusion>
                <artifactId>C</artifactId>
                <groupId>cgroupId</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
      <artifactId>C</artifactId>
        <groupId>cgroupId</groupId>
        <version>cverison</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>

      

You can now use the dependency plugin to unpack the C JAR appropriately (excluding certain files)

0


source







All Articles