Intellij IDEA doesn't recognized XJC task attr

2019-05-11 16:32发布

I have an ant build.xml file with XJC task definition:

<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
  <classpath>
    <fileset dir="jaxb" includes="*.jar" />
  </classpath>
</taskdef>

jaxb dir cotnains jaxb-xjc.jar with XJCTask class inside.

Then I call xjc task in some target:

<target name="mytarget">
 <xjc target="src" package="com.p1.Person" header="false">
   <schema dir="src/com/p1" includes="Person.xsd"/>
 </xjc>
</target>

Intellij IDEA doesn't recognize the structure/schema of the xjc call and highlights all attributes (target, package, header) and containing elements (schema) in red.

If I choose Ant options and add jaxb-xjc.jar to additional class path list this doesn't help. I use bundled Ant 1.8.2

The bad thing is that when I compile it in IDEA I get a lot of related errors, but when I run build script everything works fine. I want to suppress these errors.

Any ideas?

1条回答
欢心
2楼-- · 2019-05-11 17:05

The answer comes from this comment in a related bug in the IDEA issue tracker. http://youtrack.jetbrains.net/issue/IDEA-11248#comment=27-57354

For the XJCTask issues with IDEA, just use XJC2Task in your taskdef.

If you look at the source of XJC2Task, it has the setters exposed so that IDEA can resolve them: http://grepcode.com/file/repo1.maven.org/maven2/com.sun.xml.bind/jaxb-xjc/2.1.13/com/sun/tools/xjc/XJC2Task.java#XJC2Task.setPackage%28java.lang.String%29

However, XJCTask is just a class to dynamically delegate to JAXB1 or JAXB2 on the fly so IDEA is unable to resolve these properties since the class you are defining in the taskdef doesn't have the setters on it. http://grepcode.com/file/repo1.maven.org/maven2/com.sun.xml.bind/jaxb-xjc/2.1.13/com/sun/tools/xjc/XJCTask.java#XJCTask.getCoreClassName%28%29

Edit: Basically in JAXB2, XJCTask doesn't actually contain the task - it delegates to the actual task XJC2Task.

Here are some better links to the source:

XJCTask in JAXB 1 http://java.net/projects/jaxb/sources/version1/content/trunk/jaxb-ri/xjc/src/com/sun/tools/xjc/XJCTask.java?rev=197

XJCTask in JAXB2 http://java.net/projects/jaxb/sources/version2/content/trunk/jaxb-ri/xjc/facade/com/sun/tools/xjc/XJCTask.java?rev=3863

XJC2Task in JAXB2 http://java.net/projects/jaxb/sources/version2/content/trunk/jaxb-ri/xjc/src/com/sun/tools/xjc/XJC2Task.java?rev=3863

If you look at your jaxb-xjc-ri-2.x-xx.jar you will see that it contains a package called "1/com/sun/tools/xjc/"

This is what gets called from the XJCTask in JAXB2 if you run your ant task with setting the version to 1.0. I expect it was put in to allow easier transitions to v2 from v1 back in the day.

XJC2Task is what is called if you are using v2.

Realistically you aren't going to set it to 1.0 so you might as just call the XJC2Task directly.

查看更多
登录 后发表回答