2

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

7
  • Make separate modules and that's it...(mulit module build) which has several modules which contains each of those packages...generating different jar's from a single source directory is a bad idea... Commented Jan 30, 2020 at 13:37
  • 1
    Hint: the maven-jar plugin does not work on .java files, but on compiled .class files found in target/classes Commented Jan 30, 2020 at 13:39
  • @khmarbaise i think my assignment is strictly targeting the fact that i dont use the modules, i thought of that but yeah... it has to be the bigger evil in this case sadly :( Commented Jan 30, 2020 at 13:47
  • @GyroGearless you were correct on the classes part, but how can i exclude the whole package, because this way i only exclude the classes but the folder stays empty in the jar? Commented Jan 30, 2020 at 13:47
  • A modular structure is a modular structure which should be represented in your build structure. If you are trying to force into a single module that will fail and will cause many issue ...and you are fighting against Maven which is a combat you will loose. Commented Jan 30, 2020 at 14:14

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

4 Comments

@Esses Boy yes as i mentioned above, this is a much cleaner and easier approach, however i must do this using only one project and not modular structure.
@AlexVulchev I've updated the answer to show how to do it with the shade plugin, I'm sire there are other ways of doing it, but this works.
Why are you using shade-plugin and creating another time the jar's ? Based on the modules there will be created different jars for each module already....
@khmarbaise because of the previous comment that the questioner insists on doing it in single project, i.e. no modules. I agree with you it should be 2 modules.
0

I have accepted the above answer as right since it's the correct way to do it, but if some crazy kid like me tries to do it the wrong way, here's how:

<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>
                            <includes>
                                <include>first/FirstMain.class</include>
                                <include>log4j.properties</include>
                                <include>pictures/12px-Commons-logo.svg.png</include>
                                <include>textFiles/first.txt</include>
                            </includes>
                        </configuration>
                    </execution>
    </plugin>

Notice to use class instead of java, since it uses the compiled extention! As for the other files, they are resources on the classpath

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.