Use JAXB (xjc) generated classes in android

寵の児 提交于 2019-12-03 12:49:56

I had to do some extra researching to make cathixx's answer work since I'm new to Ant, so I'll share this to help others.

These instructions will take Java files with code like:

import javax.xml.bind.annotation.XmlElement;

@XmlRootElement
public class Response {...

...and comment these occurrences out, so it looks like:

/*import javax.xml.bind.annotation.XmlElement;*/

/*@XmlRootElement*/
public class Response {...

First, create a file build.xml (or whatever you want to call it - must be .xml) in a new Eclipse project (a "General" project is fine).

Then, add the following text to the build.xml file:

<?xml version="1.0"?>
<project
    name="CommentOutXmlAnnotations"
    basedir="."
    default="commentOutXmlAnnotations" >

<!-- This Ant script comments out the following lines from the Java files in this directory:
    import javax.xml.bind.annotation.*;
    @Xml*
 -->
    <target
        name="commentOutXmlAnnotations"        
        description="Run" >
            <replaceregexp
                byline="false"
                flags="g" >
                <regexp pattern="(@Xml[A-Za-z0-9]+(\([^)]+\))?|import javax\.xml\.bind\.annotation\.[A-Za-z0-9.]+;)[ \t]*(\r?\n)" />
                <substitution expression="/*\1*/\3" />
                <fileset dir="." >
                    <include name="*.java" />
                </fileset>
            </replaceregexp>        
    </target> 
</project>

Put the *.java files you want to comment out the XML imports and annotations for in the same directory as the build.xml file.

Right-click on the build.xml file in Eclipse, and click "Run As->Ant Build".

You should see output like:

Buildfile: D:\Eclipse_Projects\StripAnnotations\build.xml
commentOutXmlAnnotations:
BUILD SUCCESSFUL
Total time: 403 milliseconds

...and the XML imports and annotations should be commented out of your files.

Done!

Note: if you want to include all *.java files in all subdirectories of the build.xml file (for example, to comment out all XML annotations/imports generated for a bunch of JAXB classes in multiple packages), change the fileset tag to:

<fileset dir="." >
    <include name="**/*.java" />
</fileset>

now, I solved it by myself by commenting all annotations with the following ant script:

<replaceregexp flags="g" byline="false">
  <regexp pattern="(@Xml[A-Za-z0-9]+(\([^)]+\))?|import javax\.xml\.bind\.annotation\.[A-Za-z0-9.]+;)[ \t]*(\r?\n)"/>
  <substitution expression="/*\1*/\3"/>
  <fileset dir="path/to/files">
    <include name="*.java"/>
  </fileset>
</replaceregexp>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!