Skip to content
Advertisement

Maven: Adding classes from war/web-inf/classes as a jar to war/web-inf/lib

I’m rather new to maven here. I have created a Maven Spring boot project with following structure –

..
<groupId>com.example</groupId>
<artifactId>MavenWeb</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>

<name>MavenWeb</name>
<description>Maven Test Web Application</description>

<dependencies>
 ..

As you can see the packaging has been set as war. As such when the war file is generated, my source code’s generated .class files are generated in the war/web-inf/classes folder. Rather than it being generated in classes folder, I’d like to generate it as jar file and maintain it in war/web-inf/lib folder.

I’m guessing I need to make use of the maven-jar-plugin for this. But I’m not sure how to move the generated jar to war/web-inf/lib directory? Is there a simpler alternative to this?

Also how can I restrict the .class files from being generated in war/web-inf/classes folder?

Would really appreciate some pointers. Many thanks.

Advertisement

Answer

This is how the build section of pom should be –

<groupId>com.example</groupId>
<artifactId>MavenWeb</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>

<name>MavenWeb</name>
<description>Maven Test Web Application</description>

<build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archiveClasses>true</archiveClasses>
                    <webResources>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                            <artifactId>${project.artifactId}</artifactId>
                            <groupId>${project.groupId}</groupId>
                            <version>${project.version}</version>
                            <file>
                                ${project.build.directory}/${project.artifactId}-${project.version}.jar
                            </file>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

This would restrict the compiled classes from being generated in classes folder while at the same time generate a thin jar of the compiled classes and add it as web-inf/lib/MavenWeb-0.0.1.jar.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement