Mas*_*ind 6 java ant eclipse-jdt ecj
我正在创建一个java代理,用于对某些类进行一些字节码修改org.eclipse.jdt.core.JDTCompilerAdapter就是其中之一.我正在使用javassit来修改一些execute()方法org.eclipse.jdt.core.JDTCompilerAdapter.所以我在我的代理项目中包含了ecj(使用gradle)
compile group: 'org.eclipse.jdt.core.compiler' ,name: 'ecj', version :'4.3.1'
Run Code Online (Sandbox Code Playgroud)
因为我需要使用ecj中的一些类.
代理的目标是拦截对execute方法的调用,修改execute方法以向我的某些类添加一些调用,以触发一些处理.
我正在针对具有2个类的Simple Java项目测试代理.该项目使用ant构建并JDTCompilerAdapter用作编译器.
这是build.xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="TestProject">
<property file="build.properties" />
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="PClasspath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="init" name="build">
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="PClasspath"/>
</javac>
</target>
<!--
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_JDT_CORE}" includes="*.jar"/>
</copy>
</target>-->
<target name="build-e" >
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
Run Code Online (Sandbox Code Playgroud)
在构建项目时将使用代理.因此,为了测试代理,我使用以下命令:
java -jar agent-wrapper.jar --outdir ./out --exec ./build_wrapper.sh
Run Code Online (Sandbox Code Playgroud)
build_wrapper.sh包含这个(我添加了ecj依赖项,所以我可以JDTCompilerAdapter像在bulid.xml中一样 编译项目<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/> :
../ant/bin/ant -lib ../eclipse/plugins/ecj-4.3.1.jar build-e
Run Code Online (Sandbox Code Playgroud)
这个想法是代理包装器将解析参数(outdir用于生成一些东西,exec是用于启动我的测试项目的构建的脚本)获取要执行的命令build_wrapper.sh(在这种情况下 ../ant/bin/ant -lib ../eclipse/plugins/ecj-4.3.1.jar build-e)并添加它自己作为命令的java代理.
在执行代理期间会发生此问题.这是输出:
java -jar custom-agent.jar --outdir ./out --exec ./build_wrapper.sh [10:18:53]
Picked up JAVA_TOOL_OPTIONS: -javaagent:/Users/dev/TestAgent/project/custom-agent.jar=OUTDIR=/Users/dev/TestAgent/project/./out
objc[30474]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Buildfile: /Users/dev/TestAgent/project/build.xml
build-e:
init:
[mkdir] Created dir: /Users/dev/TestAgent/project/bin
build:
BUILD FAILED
/Users/dev/TestAgent/project/build.xml:47: The following error occurred while executing this line:
/Users/dev/TestAgent/project/build.xml:32: Class org.eclipse.jdt.core.JDTCompilerAdapter could not be loaded because of an invalid dependency.
Total time: 2 seconds
abnormal termination, exit code: 1
Run Code Online (Sandbox Code Playgroud)
当我不在我的代理项目中使用ecj-4.3.1.jar时,构建运行良好我拦截了对execute()方法的调用但我不能使用ecj jar中的其他类.
显示停止错误是“由于依赖项无效,无法加载类 org.eclipse.jdt.core.JDTCompilerAdapter”。
阅读此链接可能会找到第一个错误提示 http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-ant_javac_adapter.htm
第二个提示可能是运行 JDTCompilerAdapter 所需的 jar 之一丢失。
为了让 JDTCompilerAdapter 正常工作,我将 JDTCompilerAdapter.jar 和 org.eclipse.jdt.core.jar 复制到 ant/lib 文件夹中。
上面提到的链接中记录了基于 eclipse 版本和 java 版本的差异。