Maven и Spring Boot: как создать плоскую банку уменьшенного размера (с помощью proguard или аналогичного) - PullRequest
0 голосов
/ 29 мая 2020

Я работаю над приложением Maven Spring Boot (без Интернета, только для рабочего стола). В проекте всего 30 классов, но использование зависимостей загрузки Spring создает в конце банку размером 13 МБ. Можно ли использовать Proguard или любой другой плагин для интеграции с Spring Boot, чтобы уменьшить размер JAR? Мой pom-файл проекта:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>dummy-app</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <url>https://github.com/xxx</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>

        <spring.boot.mainclass>oorg.example.Application</spring.boot.mainclass>

        <antlr.version>4.7.1</antlr.version>
        <junit-jupiter.version>5.5.2</junit-jupiter.version>
        <hamcrest.version>2.2</hamcrest.version>

        <proguard.maven.plugin.version>2.2.0</proguard.maven.plugin.version>
        <proguard.version>6.2.0</proguard.version>
    </properties>

    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>


    <build>
        <plugins>
            <plugin>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4-maven-plugin</artifactId>
                <version>${antlr.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>antlr4</goal>
                        </goals>
                        <configuration>
                            <listener>true</listener>
                            <visitor>true</visitor>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <!-- https://mvnrepository.com/artifact/org.antlr/antlr4-runtime -->
                    <dependency>
                        <groupId>org.antlr</groupId>
                        <artifactId>antlr4-runtime</artifactId>
                        <version>${antlr.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.eluder.coveralls</groupId>
                <artifactId>coveralls-maven-plugin</artifactId>
                <version>4.3.0</version>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <finalName>Example</finalName>
                            <mainClass>${spring.boot.mainclass}</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
            </plugin>
        </plugins>
    </reporting>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.antlr/antlr4-runtime -->
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-runtime</artifactId>
            <version>${antlr.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>${hamcrest.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

Я пытаюсь интегрироваться с плагином proguard, но не могу найти способ заставить его работать. Есть идеи для выполнения sh этой задачи?

<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <version>${proguard.maven.plugin.version}</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <proguardVersion>${proguard.version}</proguardVersion>
        <injar>${project.build.finalName}.jar</injar>
        <outjar>suca${project.build.finalName}.jar</outjar>
        <obfuscate>true</obfuscate>
        <options>
<!--                        <option>-shrink</option>-->
<!--                        <option>-optimize</option>-->
            <!-- This option will replace all strings in reflections method invocations with new class names.
                 For example, invokes Class.forName('className')-->
            <option>-adaptclassstrings</option>
            <!-- This option will save all original annotations and etc. Otherwise all we be removed from files.-->
            <option>-keepattributes
                Exceptions,
                InnerClasses,
                Signature,
                Deprecated,
                SourceFile,
                LineNumberTable,
                *Annotation*,
                EnclosingMethod
            </option>
            <!-- This option will save all original names in interfaces (without obfuscate).-->
            <option>-keepnames interface **</option>
            <!-- This option will save all original methods parameters in files defined in -keep sections,
                 otherwise all parameter names will be obfuscate.-->
            <option>-keepparameternames</option>
            <!-- This option will save all original class files (without obfuscate) but obfuscate all
                 in domain and service packages.-->
            <option>-keep
                class ${spring.boot.mainclass} {
                public static void main(java.lang.String[]);
                }
            </option>
            <!-- This option ignore warnings such as duplicate class definitions and classes in incorrectly
                named files-->
            <option>-ignorewarnings</option>
            <!-- This option will save all original class files (without obfuscate) in service package-->
            <!--<option>-keep class com.slm.proguard.example.spring.boot.service { *; }</option>-->
            <!-- This option will save all original interfaces files (without obfuscate) in all packages.-->
            <option>-keep interface * extends * { *; }</option>
            <!-- This option will save all original defined annotations in all class in all packages.-->
            <option>-keepclassmembers class * {
                @org.springframework.beans.factory.annotation.Autowired *;
                @org.springframework.beans.factory.annotation.Value *;
                }
            </option>
        </options>
        <injarNotExistsSkip>true</injarNotExistsSkip>
        <libs>
            <!--Put here your libraries if required-->
            <!--<lib>${java.home}/lib/rt.jar</lib>-->
        </libs>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>net.sf.proguard</groupId>
            <artifactId>proguard-base</artifactId>
            <version>${proguard.version}</version>
        </dependency>
    </dependencies>
</plugin>

Примечание: у всех зависимостей есть почти класс, который я использую в своем проекте, я не могу их удалить.

...