[Maven] Maven plugin으로 난독화 적용하기

37944 단어 JObf난독화mavenJObf

목표

JObf 를 빌드된 jar 파일에 적용하고 배포하는 것을 maven plugin을 통해 자동화할 수 있다.

해결 방안

1. os 별 실행 파일 지정

<profiles>
  <profile>
    <id>Windows</id>
    <activation>
      <os>
        <family>Windows</family>
      </os>
    </activation>
    <properties>
      <script.to.execute>${project.basedir}/obfuscate.bat</script.to.execute>
      <script.executor>powershell.exe</script.executor>
    </properties>
  </profile>
  <profile>
    <id>unix</id>
    <activation>
      <os>
        <family>unix</family>
      </os>
    </activation>
    <properties>
      <script.to.execute>${project.basedir}/obfuscate.sh</script.to.execute>
      <script.executor>bash</script.executor>
    </properties>
  </profile>
</profiles>

obfuscate.bat

echo on
java -jar obfuscator.jar --jarIn target/project-*-SNAPSHOT.jar --jarOut target/project-obf.jar --config config.jocfg

obfuscate.sh

#!/bin/sh
java -jar obfuscator.jar --jarIn target/project-*-SNAPSHOT.jar --jarOut target/project-obf.jar --config config.jocfg

windows 환경에서는 obfuscate.bat 을 linux 환경에서는 obfuscate.sh 을 실행하도록 profile-activation-os 를 지정한다.

2. 난독화 적용

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    <execution>
      <id>obfuscate</id>
      <phase>package</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <executable>${script.executor}</executable>
        <commandlineArgs>${script.to.execute}</commandlineArgs>
      </configuration>
    </execution>
  </executions>
</plugin>

package 된 jar 파일에 obfuscate.jar 을 실행해 난독화가 적용된 jar를 생성한다.

3. 난독화 된 jar 파일 배포

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-install-plugin</artifactId>
  <version>3.0.0-M1</version>
  <executions>
    <execution>
      <goals>
        <goal>install</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <skip>true</skip>
  </configuration>
</plugin>

난독화가 적용되지 않은 jar 파일의 install 과정을 skip

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-install-plugin</artifactId>
  <version>3.0.0-M1</version>
  <executions>
    <execution>
      <goals>
        <goal>install-file</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <file>target/project-obf.jar</file>
    <groupId>${project.groupId}</groupId>
    <artifactId>${project.artifactId}</artifactId>
    <version>${project.version}</version>
    <packaging>jar</packaging>
  </configuration>
</plugin>

난독화가 적용된 jar 파일을 대신 install 한다

mvn clean install install:install-file
명령 실행 시 난독화가 적용된 jar 파일이 local repository에 등록되는 것을 확인할 수 있다.

전체 pom.xml

<profiles>
  <profile>
    <id>Windows</id>
    <activation>
      <os>
        <family>Windows</family>
      </os>
    </activation>
    <properties>
      <script.to.execute>${project.basedir}/obfuscate.bat</script.to.execute>
      <script.executor>powershell.exe</script.executor>
    </properties>
  </profile>
  <profile>
    <id>unix</id>
    <activation>
      <os>
        <family>unix</family>
      </os>
    </activation>
    <properties>
      <script.to.execute>${project.basedir}/obfuscate.sh</script.to.execute>
      <script.executor>bash</script.executor>
    </properties>
  </profile>
</profiles>
<build>
  ...
  <plugins>
    ...
    <plugin>
      <artifactId>exec-maven-plugin</artifactId>
      <groupId>org.codehaus.mojo</groupId>
      <version>3.0.0</version>
      <executions>
        <execution>
          <id>obfuscate</id>
          <phase>package</phase>
          <goals>
            <goal>exec</goal>
          </goals>
          <configuration>
            <executable>${script.executor}</executable>
            <commandlineArgs>${script.to.execute}</commandlineArgs>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-install-plugin</artifactId>
      <version>3.0.0-M1</version>
      <executions>
        <execution>
          <goals>
            <goal>install</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <skip>true</skip>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-install-plugin</artifactId>
      <version>3.0.0-M1</version>
      <executions>
        <execution>
          <goals>
            <goal>install-file</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <file>target/project-obf.jar</file>
        <groupId>${project.groupId}</groupId>
        <artifactId>${project.artifactId}</artifactId>
        <version>${project.version}</version>
        <packaging>jar</packaging>
      </configuration>
    </plugin>
  </plugins>
</build>

참고
https://stackoverflow.com/questions/3491937/i-want-to-execute-shell-commands-from-mavens-pom-xml
https://ilhicas.com/2019/04/19/Maven-exec-os-dependent.html

좋은 웹페이지 즐겨찾기