Warm tip: This article is reproduced from stackoverflow.com, please click
java maven jar maven-plugin

creating a jar for every java package in java project

发布于 2020-03-27 15:34:33

lets say i have 3 packages and a jar needs to be created for every package containing only the contents in the current package. My attempt is:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>first-jar</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>first-jar</classifier>
                            <excludes>
                                <exclude>/maven.task.3/src/main/java/third/ThirdMain.java
                                </exclude>
                                <exclude>/maven.task.3/src/main/java/second/SecondMain.java
                                </exclude>
                            </excludes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>second-jar</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>second-jar</classifier>
                            <excludes>
                                <exclude>/maven.task.3/src/main/java/first/FirstMain.java
                                </exclude>
                                <exclude>/maven.task.3/src/main/java/third/ThirdMain.java
                                </exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

And this does indeed create different jars, however the content inside is the same, meaning the exclude clauses don't work. I tried excluding only the class(relative/absolute path) and only the package. Don't ask why i do this its for homework, it does not make a lot of sense! This is the way i attempt of doing it, if there is any other more efficient way please feel free to share it with me!

EDIT: Modular structure must not be used, it must be one single project.

Thanks in advance

Questioner
Alex Vulchev
Viewed
56
Essex Boy 2020-01-30 22:29

It's already been answered in the comments but here's an example.

A parent pom:

<?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.essexboy</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>jar1</module>
    <module>jar2</module>
  </modules>

</project>

And 2 child/modules poms, only the artifactId is different:

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

  <parent>
    <artifactId>parent</artifactId>
    <groupId>com.essexboy</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>jar1</artifactId>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
  </build>
</project>

Extra info

I created the parent with the command:

mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE

Then created 2 modules with the command

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE

If you must have a single jar project (no modules and parent) you can do it with the shade-plugin:

<?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.essexboy</groupId>
    <artifactId>double-jar</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>double-jar</name>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>1</id>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>jar1</finalName>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>com/essexboy/App2*</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                    <execution>
                        <id>2</id>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>jar2</finalName>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>com/essexboy/App1*</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>