1

We are trying to the obfuscation to Spring Boot Application. For obfuscation we are using proguard

The configuration i did is

<build>
    <plugins>

        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>proguard</id>
                    <phase>package</phase>
                    <goals>
                        <goal>proguard</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- File with proguard configuration -->
                <proguardInclude>${basedir}/build/proguard.conf</proguardInclude>
                <obfuscate>true</obfuscate>
                <includeDependency>true</includeDependency>
                <injar>${project.build.finalName}.${project.packaging}</injar>
                <outjar>${project.build.finalName}-proguard.${project.packaging}</outjar>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>net.sf.proguard</groupId>
                    <artifactId>proguard-base</artifactId>
                    <version>${version.net.sf.proguard}</version>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.Application</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>

                    </goals>

                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

When i am trying to build the the application it is creating the METAINF.MF file in actual war file but not in the obfuscated war file.

Also i have provid maven-assembly-plugin plugin before providing the obfuscation and spring assembly package.

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>assembly</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                </execution>
            </executions>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.Application</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
                <attach>false</attach>
            </configuration>
        </plugin>

Can anyone suggest what i additional configuration i need to provide.

I have tried the permutation of rearraging the plugins but no success.

Is there way to define the archive manifest attribute in spring boot maven plugin?

mahesh
  • 909
  • 2
  • 18
  • 37

1 Answers1

0

We have changed the approach now.

By simply obfuscating the package wont solve this issue. After this as well , you will probably Steps:

  1. First we are creating the war using maven-war-plugin
  2. Unpack the war and keep the files which you need to obfuscate in one folder and jar in another folder
  3. Use the maven ant plugin to pack it again.
<build>
        <finalName>Application</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.Application</mainClass>
                        </manifest>
                    </archive>
                    <appendAssemblyId>false</appendAssemblyId>
                    <attach>false</attach>
                </configuration>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <version>2.0.14</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <proguardVersion>5.2.1</proguardVersion>
                    <injar>Application/WEB-INF/lib</injar>
                    <outjar>Application/WEB-INF-OB/lib</outjar>
                    <proguardInclude>${basedir}/proguard.conf</proguardInclude>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>antbuild</id>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <property name="build.compiler" value="extJavac" />
                                <ant dir="${basedir}" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant</artifactId>
                        <version>${ant.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.ivy</groupId>
                        <artifactId>ivy</artifactId>
                        <version>2.4.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-antlib</artifactId>
                        <version>2.0.2.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Ant script:

<project xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:spring-boot="antlib:org.springframework.boot.ant" name="spring-boot-sample-ant" default="build">

    <description>
        Sample ANT build script for a Spring Boot executable JAR project. Uses ivy for
        dependency management and spring-boot-antlib for additional tasks. Run with
        '$ ant -lib ivy-2.2.jar spring-boot-antlib.jar' (substitute the location of your
        actual jars). Run with '$ java -jar target/*.jar'.
    </description>

    <property name="ant-spring-boot.version" value="${revision}" />
    <property name="lib.dir" location="${basedir}/target/lib" />
    <property name="start-class" value="com.Application" />

    <target name="resolve" description="--> retrieve dependencies with ivy">
        <ivy:retrieve pattern="${lib.dir}/[conf]/[artifact]-[type]-[revision].[ext]" />
    </target>

    <target name="classpaths" depends="resolve">
        <path id="compile.classpath">
            <fileset dir="${lib.dir}/compile" includes="*.jar" />
        </path>
    </target>

    <target name="init" depends="classpaths">
        <mkdir dir="target/classes" />
    </target>

    <target name="compile" depends="init" description="compile">
        <javac srcdir="src/main/java" destdir="Application/WEB-INF/classes" classpathref="compile.classpath" />
    </target>

    <target name="clean" description="cleans all created files/dirs">
        <delete dir="target" />
    </target>

    <target name="build">
        <spring-boot:exejar destfile="target/${ant.project.name}-${ant-spring-boot.version}.jar" classes="target/Application/WEB-INF/classes">
            <spring-boot:lib>
                <fileset dir="target/Application/WEB-INF-OB/lib" />
            </spring-boot:lib>
        </spring-boot:exejar>
    </target>
    <!-- Manual equivalent of the build target -->
    <target name="manual">
        <war destfile="target/${ant.project.name}-${ant-spring-boot.version}.war" compress="false">
            <mappedresources>
                <fileset dir="target/Application/WEB-INF/classes" />
                <globmapper from="*" to="WEB-INF/classes/*" />
            </mappedresources>
            <mappedresources>
                <fileset dir="src/main/resources" erroronmissingdir="false" />
                <globmapper from="*" to="WEB-INF/classes/*" />
            </mappedresources>
            <mappedresources>
                <fileset dir="target/Application/WEB-INF/lib" />
                <globmapper from="*" to="WEB-INF/lib/*" />
            </mappedresources>
            <zipfileset src="target/Application/WEB-INF/lib/spring-boot-loader-tools-1.5.9.RELEASE.jar" />
            <manifest>
                <attribute name="Main-Class" value="org.springframework.boot.loader.JarLauncher" />
                <attribute name="Start-Class" value="${start-class}" />
            </manifest>
        </war>
    </target>
</project>

This should probably solve the problem.

mahesh
  • 909
  • 2
  • 18
  • 37