java - Spring 启动:unable to open nested entry 'WEB-INF/lib-provided/ecj-3.12.3.jar'

标签 java spring-boot ant obfuscation yguard

我正在尝试混淆 Spring Boot 应用程序。 套餐: war

我使用 yguard 遵循 ant 脚本方法

链接:http://codeaweso.me/2009/02/obfuscating-a-webapp-war-file-with-yguard-and-ant/

项目结构:

main.war

  • a.jar

  • b.jar

以及更多第三方依赖项。

我可以看到我们已经成功混淆了所需的第三方依赖项 jar 文件。

此外,我们还保留了其他库的未混淆和未压缩状态,但介绍了它们的压缩方式。

我们得到的异常是

java -jar webapp_obf.war
Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry WEB-INF/lib-provided/ecj-3.12.3.jar
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:109)
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:87)
        at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:72)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:49)
        at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:59)
Caused by: java.io.IOException: Unable to open nested jar file 'WEB-INF/lib-provided/ecj-3.12.3.jar'
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:252)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:237)
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:104)
        ... 4 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'WEB-INF/lib-provided/ecj-3.12.3.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
        at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:285)
        at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:260)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:248)
        ... 6 more

<project xmlns='antlib:org.apache.tools.ant'>
	<!-- prepare a temporary directory in which the war file is expanded and 
		obfuscated -->
	<tempfile property="unwar.dir" destdir="obfuscation/"
		deleteonexit="false" />
	<mkdir dir="${unwar.dir}" />
	<unwar src="../target/webapp.war"
		dest="${unwar.dir}" />

	<!-- create a jar of webapp classes (required by yguard) for obfuscation -->
	<jar destfile="${unwar.dir}/WEB-INF/lib/webapp.jar"
		whenempty="fail">
		<zipfileset dir="${unwar.dir}/WEB-INF/classes" />
		<zipfileset dir="${unwar.dir}/META-INF" />

	</jar>

	<!-- <delete dir="${unwar.dir}/WEB-INF/classes" /> -->


	<!-- create a fileset of internal libraries to be obfuscated -->
	<fileset dir="${unwar.dir}/WEB-INF/lib" id="internal.lib.set">
		<include name="first.jar" />
		<include name="second.jar" />
	
	</fileset>

	<!-- move the internal libraries to a temporary directory and make a fileset 
		out of them -->
	<tempfile property="obfuscation.dir"
		destDir="obfuscation/temp" deleteonexit="false" />
	<mkdir dir="${obfuscation.dir}" />
	<move todir="${obfuscation.dir}">
		<fileset refid="internal.lib.set" />
	</move>

	<!-- create a jar of web.xml (required by yguard) for obfuscation -->
	<jar destfile="${obfuscation.dir}/web.xml.jar" whenempty="fail">
		<zipfileset dir="${unwar.dir}/WEB-INF" includes="web.xml" />
	</jar>
	<!-- <delete file="${unwar.dir}/WEB-INF/web.xml" /> -->

	<!-- make a fileset of all jars to be obfuscated -->
	<fileset dir="${obfuscation.dir}" includes="*.jar"
		id="in-out.set" />

	<!-- make a fileset of the remaining libraries, these are not obfuscated -->
	<path id="external.lib.path">
		<fileset dir="${unwar.dir}/WEB-INF/lib" includes="*.jar" />
		<fileset dir="${unwar.dir}/WEB-INF/lib-provided" includes="*.jar" />
	</path>

	<taskdef name="yguard" classname="com.yworks.yguard.YGuardTask"
		classpath="yguard.jar" />

	<yguard>
		<inoutpairs>
			<!-- these filesets are inputs to be obfuscated -->
			<fileset refid="in-out.set" />
		</inoutpairs>
		<externalclasses refid="external.lib.path" />  <!-- external libs, not obfuscated -->
		<rename>
			<adjust replaceContent="true">
				<include name="web.xml" />  <!-- modified to reference the obfuscated Servlet -->
			</adjust>
			<keep>
				<!-- classes, packages, methods, and fields which should not obfuscated 
					are specified here -->
				<class classes="none" methods="none" fields="none" >
					<patternset>
						<include name="com.Application" />
					</patternset>
				</class>

			</keep>
		</rename>
	</yguard>

	<!-- move our newly obfuscated classes back into the lib area -->
	<move todir="${unwar.dir}/WEB-INF/lib">
		<fileset dir="${obfuscation.dir}" includes="*_obf.jar" />
	</move>

	<!-- unjar the adjusted web.xml -->
	<unzip dest="${unwar.dir}/WEB-INF/"
		src="${unwar.dir}/WEB-INF/lib/web.xml_obf.jar">
		<patternset includes="web.xml" />
	</unzip>
	<delete>
		<fileset dir="${unwar.dir}/WEB-INF/lib"
			includes="web.xml*.jar" />
	</delete>

	<!-- rebuild the war file -->
	<war destfile="webapp_obf.war" basedir="${unwar.dir}"
		needxmlfile='false'>
		<manifest>
			<attribute name="Main-Class"
				value="org.springframework.boot.loader.WarLauncher" />
			<attribute name="Start-Class"
				value="com.Application" />
			<attribute name="Spring-Boot-Classes"
				value="WEB-INF/classes/" />
			<attribute name="Spring-Boot-Lib" value="WEB-INF/lib/" />
			<attribute name="Build-Jdk" value="1.8.0_171" />
		</manifest>
	</war>
</project>

任何关于我们可能出现压缩异常的建议。

最佳答案

当您尝试构建 jar 时,只需提供以下选项。

ma​​ven 组件插件

 <archive> 
<compress>false</compress>
 </archive>

我们也可以在 spring-boot maven 插件中提供相同的功能。

关于java - Spring 启动:unable to open nested entry 'WEB-INF/lib-provided/ecj-3.12.3.jar' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51095500/

相关文章:

java - 正则表达式从文件名中删除空格

maven - 如何从 maven 获取实际的输出文件名

java - 无法加载ApplicationContext(Spring Boot)

oracle - spring boot应用需要连接weblogic oracle数据源

java - 如何让 ant4eclispe 构建一个包含所有依赖项的项目?

java - 在 Eclipse Indigo 中没有从现有 Ant 构建文件导入 Java 项目的选项

java - 同时按下按键时未接收到按键事件

java - org.w3c.dom.Node 的并发和复用

java - Logstash stdout - 写入文件

java - 如何在同一个查询中检查多个 ID?