2

I have a simple Java Spring MVC web app. I build the WAR file using a Maven pom.xml file.

When I do a "maven install", it produces a WAR file with my compiled Java classes in the /WEB-INFO/classes folder.

My maven pom.xml looks like this (dependencies commented out) ...

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>mycompany.com</groupId>
    <artifactId>myapp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>My Application</name>
    <url>http://maven.apache.org</url>
    <dependencies>

<!-- .... -->

    </dependencies>
    <build>
        <finalName>myapp</finalName>
    </build>
</project>

Next I want to obfuscate my Java classes (I'll be using Allatori obfuscater), so I'm thinking the easiest thing would be if my Java classes were all put into their own JAR file and stored in the /WEB-INF/lib folder with the rest of the JARs.

Is there a way to modify my Maven pom.xml file so it will package of my classes up in a JAR and put the JAR in the /WEB-INF/lib folder?

UPDATE:

Adding this to the 'build' section (as suggested by "JF Meier" worked) ...

    <build>
        <finalName>myapp</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archiveClasses>true</archiveClasses>
                </configuration>
            </plugin>
        </plugins>

    </build>
AndySummers2020
  • 411
  • 1
  • 6
  • 14

2 Answers2

2

You can use the <archiveClasses> configuration (set it to true) to generate an additional jar with the classes.

Also see:

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

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
0

Move your Java classes in a separate Maven module and add that as a dependency to your WAR project. Then you can do whatever is necessary creating the jar in that module.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Not sure how to do this ... would I have two Eclipse projects each with a pom.xml. One project which produces the obfuscated JAR file, and second project produces the WAR file and uses the JAR file as a dependency? Or is there another way to do that? – AndySummers2020 Jun 14 '20 at 01:33