JObf 를 빌드된 jar 파일에 적용하고 배포하는 것을 maven plugin을 통해 자동화할 수 있다.
<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 를 지정한다.
<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를 생성한다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
<executions>
<execution>
<id>default-install</id>
<goals>
<goal>install</goal>
<goal>install-file</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
<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 과정을 skip 하고
난독화가 적용된 jar 파일을 대신 install 한다.
execution id 를 default-install 로 설정 시, mvn install
명령으로 실행되는 동작을 덮어쓸 수 있다.
mvn clean install
명령 실행 시 난독화가 적용된 jar 파일이 local repository에 등록되는 것을 확인할 수 있다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<goals>
<goal>deploy</goal>
<goal>deploy-file</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
<repositoryId>${project.distributionManagement.repository.id}</repositoryId>
<url>${project.distributionManagement.repository.url}</url>
<file>target/project-obf.jar</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</configuration>
</plugin>
난독화가 적용되지 않은 jar 파일의 deploy 과정을 skip 하고
난독화가 적용된 jar 파일을 대신 deploy 한다.
execution id 를 default-deploy 로 설정 시, mvn deploy
명령으로 실행되는 동작을 덮어쓸 수 있다.
<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>
<id>default-install</id>
<goals>
<goal>install</goal>
<goal>install-file</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
<file>target/project-obf.jar</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<goals>
<goal>deploy</goal>
<goal>deploy-file</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>true</skip>
<repositoryId>${project.distributionManagement.repository.id}</repositoryId>
<url>${project.distributionManagement.repository.url}</url>
<file>target/project-obf.jar</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</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