Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

That's all I need. Additional details: I have a src/bootstrap/java folder and the regular src/main/java folder. Each one needs to go to a separate jar for obvious reasons. I was able to generate a bootstrap jar using this:

  <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
      <execution>
        <id>only-bootstrap</id>
        <goals><goal>jar</goal></goals>
        <phase>package</phase>
        <configuration>
          <classifier>bootstrap</classifier>
          <includes>
            <include>sun/**/*</include>
          </includes>
        </configuration>
      </execution>
    </executions>
  </plugin>

But the regular jar still includes the bootstrap classes. I am compiling the bootstrap classes with this answer.

Any light to generate a myproject.jar WITHOUT the bootstrap classes?

like image 238
chrisapotek Avatar asked Mar 17 '12 20:03

chrisapotek


People also ask

How do I exclude properties from a jar file?

xml file to exclude property files. You need to specify the directory where the files are reside in your application structure. Also you need to specify the file types to be excluded using exclude tag. Below code excludes any file name ending with .

Can you exclude a class from a jar?

How to include/exclude content from jar artifact. Specify a list of fileset patterns to be included or excluded by adding <includes> / <include> or <excludes> / <exclude> in your pom. xml . Note that the patterns need to be relative to the path specified for the plugin's classesDirectory parameter.

Can we exclude a class from Maven dependency?

Since Maven resolves dependencies transitively, it is possible for unwanted dependencies to be included in your project's classpath. For example, a certain older jar may have security issues or be incompatible with the Java version you're using. To address this, Maven allows you to exclude specific dependencies.


2 Answers

I think you can take a look at this before deciding to generate two jars from one pom.

Maven best practice for generating multiple jars with different/filtered classes?

If you still decide to get two jars, you can probably do it using this. You have to specify the proper exclusions.

<build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
          <execution>
            <id>only-bootstrap</id>
            <goals><goal>jar</goal></goals>
            <phase>package</phase>
            <configuration>
              <classifier>only-library</classifier>
              <includes>
                <include>**/*</include>
              </includes>
              <excludes>
                <exclude>**/main*</exclude>
              </excludes>
            </configuration>
          </execution>
          <execution>
            <id>only-main</id>
            <goals><goal>jar</goal></goals>
            <phase>package</phase>
            <configuration>
              <classifier>everything</classifier>
              <includes>
                <include>**/*</include>
              </includes>
              <excludes>
                <exclude>**/bootstrap*</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
like image 99
r3st0r3 Avatar answered Oct 16 '22 16:10

r3st0r3


You gotta use "default-jar" for the ID:

  <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
      <execution>
        <id>only-bootstrap</id>
        <goals><goal>jar</goal></goals>
        <phase>package</phase>
        <configuration>
          <classifier>bootstrap</classifier>
          <includes>
            <include>sun/**/*</include>
          </includes>
        </configuration>
      </execution>
      <execution>
        <id>default-jar</id>
        <phase>package</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        <configuration>
          <excludes>
            <exclude>sun/**/*</exclude>
          </excludes>
        </configuration>
      </execution>
    </executions>
  </plugin>
like image 35
TraderJoeChicago Avatar answered Oct 16 '22 15:10

TraderJoeChicago