Maven Create Jar and exclude Files

Puja

I am trying to create a runnable Jar file. I have to create a Jar of only one single java class (MyClass which is under the folder structure src/main/java/org/miso/mcs/util/MyClass) and exclude others. I am able to create to JAR but it has everything.

I am using mvn clean package assembly:single command to create Jar.

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>org.miso.mcs.util.MyClass</mainClass>
      </manifest>
    </archive>  
    <resources>
      <resource>
        <classesDirectory>src/main/java/org/miso/mcs/util</classesDirectory>
        <includes>
          <include>/*MyClass*</include>
        </includes>
        <excludes>
          <exclude>/*</exclude>
        </excludes>
      </resource>
    </resources>               
  </configuration>
</plugin>
Tunaki

You won't be able to do that by using the predefined jar-with-dependencies descriptor. However, we can make our own assembly descriptor for that:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>false</useProjectArtifact>
      <unpack>true</unpack>
    </dependencySet>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>false</useProjectArtifact>
      <useProjectAttachments>true</useProjectAttachments>
      <includes>
        <include>${project.groupId}:${project.artifactId}:jar:classes:${project.version}</include>
      </includes>
      <unpack>true</unpack>
      <unpackOptions>
        <excludes>
          <exclude>%regex[org\/miso\/(?!mcs\/util\/MyClass).*]</exclude>
        </excludes>
      </unpackOptions>
    </dependencySet>
  </dependencySets>
</assembly>

unpackOptions allows to control the content the unpacked dependencies in the JAR. In this case, we are excluding everything under the org.miso package (which I assume are the classes of your project) and we're only including the class you're interested in (org.miso.mcs.util.MyClass). This will result in a runnable JAR with only the specified class of your project.

Since your project is a WAR, it is a little more complicated. The first dependency set declares all the dependencies of the project and unpacks them. The second only uses the projects attachments and includes only the classes attachment built by the maven-war-plugin.

If you save that snippet into a file called assembly.xml located under src/assembly, your POM will then look like:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <attachClasses>true</attachClasses>
  </configuration>
</plugin>
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.6</version>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>src/assembly/assembly.xml</descriptor>
        </descriptors>
        <archive>
          <manifest>
            <mainClass>org.miso.mcs.util.MyClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </execution>
  </executions>
</plugin>

With such a configuration, you just need to invoke Maven with mvn clean install and it will correctly generate the JAR. Then, you can launch that JAR with java -jar name-of-jar.jar.

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-dependency-plugin: exclude .jar files

From Dev

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

From Dev

maven: exclude jar from JDK

From Dev

Create a tar with maven generated jar with dependenceis and few other files

From Dev

Why does Maven + Spring Boot create huge jar-files?

From Dev

How to create .bat files using Maven Appassembler that runs a JAR?

From Dev

Why does Maven + Spring Boot create huge jar-files?

From Dev

How to create .bat files using Maven Appassembler that runs a JAR?

From Dev

When building an application jar with Maven Shade, is it safe to exclude all META-INF/versions directories/files?

From Dev

Exclude packages while generating sources jar in Maven

From Dev

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

From Dev

Maven - How to exclude dependencies from generated JAR

From Dev

How to exclude resource files when lein jar?

From Dev

Maven creates empty jar files

From Dev

Specifying specific jar files with maven

From Dev

Maven - Exclude class files from war

From Dev

Maven - Exclude class files from war

From Dev

Create JAR with one file in Maven

From Dev

Create jar file from maven

From Dev

Create JAR with one file in Maven

From Dev

I wish to exclude some class files from my jar. I am using maven-assembly-plugin. It still adds the files. I dont get any error

From Dev

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

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

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

From Dev

Trying to Create a TAR Archive, But Exclude .messages Files

From Java

How to add local jar files to a Maven project?

From Dev

maven - where to store external jar files if needed

From Dev

Include specific JAR files in skinny WARs with Maven

Related Related

  1. 1

    maven-dependency-plugin: exclude .jar files

  2. 2

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

  3. 3

    maven: exclude jar from JDK

  4. 4

    Create a tar with maven generated jar with dependenceis and few other files

  5. 5

    Why does Maven + Spring Boot create huge jar-files?

  6. 6

    How to create .bat files using Maven Appassembler that runs a JAR?

  7. 7

    Why does Maven + Spring Boot create huge jar-files?

  8. 8

    How to create .bat files using Maven Appassembler that runs a JAR?

  9. 9

    When building an application jar with Maven Shade, is it safe to exclude all META-INF/versions directories/files?

  10. 10

    Exclude packages while generating sources jar in Maven

  11. 11

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

  12. 12

    Maven - How to exclude dependencies from generated JAR

  13. 13

    How to exclude resource files when lein jar?

  14. 14

    Maven creates empty jar files

  15. 15

    Specifying specific jar files with maven

  16. 16

    Maven - Exclude class files from war

  17. 17

    Maven - Exclude class files from war

  18. 18

    Create JAR with one file in Maven

  19. 19

    Create jar file from maven

  20. 20

    Create JAR with one file in Maven

  21. 21

    I wish to exclude some class files from my jar. I am using maven-assembly-plugin. It still adds the files. I dont get any error

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

    Trying to Create a TAR Archive, But Exclude .messages Files

  27. 27

    How to add local jar files to a Maven project?

  28. 28

    maven - where to store external jar files if needed

  29. 29

    Include specific JAR files in skinny WARs with Maven

HotTag

Archive