编程语言
首页 > 编程语言> > java – 从jar中为类文件生成jaxb Xsd模式

java – 从jar中为类文件生成jaxb Xsd模式

作者:互联网

我想从jar中的类文件生成jaxb xsd模式.目前,我使用jaxb2-maven-plugin使用java文件生成模式.

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>${maven.plugin.jaxb2}</version>
        <executions>
           <execution>
              <id>schemagen</id>
              <goals>
                 <goal>schemagen</goal>
              </goals>
              <phase>process-classes</phase>
              <configuration>
                 <quiet>true</quiet>
                 <includes>
                    <include>com/someProject/domain/*.java</include>
                 </includes>
                 <outputDirectory>${project.build.directory}/schemas</outputDirectory>
                 <clearOutputDir>true</clearOutputDir>
              </configuration>
           </execution>
        </executions>
     </plugin>

但是,我有一个用例,我得到一个依赖jar文件,并希望从该jar文件中生成一些类.任何人都可以建议如何做到这一点.

解决方法:

为简单起见,我重用代码presented in this article来演示从存储在Jar-archive中的Java类生成Jaxb Schema的必要步骤

代码由两个类组成 – 员工和地址:

package base.package;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employee")
public class Employee 
{
    @XmlAttribute
    private int id;
    private String name;
    private double salary;
    private String designation;
    private Address address;

    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public double getSalary() { return salary; }
    public void setSalary(double salary) { this.salary = salary; }
    public String getDesignation() { return designation; }
    public void setDesignation(String designation) { this.designation = designation; }
    public Address getAddress() { return address; }
    public void setAddress(Address address) { this.address = address; }
}

和引用的类:

package base.package;

public class Address 
{
    private String line1;
    private String line2;
    private String city;
    private String state;
    private long zipcode;

    public String getLine1() { return line1; }
    public void setLine1(String line1) { this.line1 = line1; }
    public String getLine2() { return line2; }
    public void setLine2(String line2) { this.line2 = line2; }
    public String getCity() { return city; }
    public void setCity(String city) { this.city = city; }
    public String getState() { return state; }
    public void setState(String state) { this.state = state; }
    public long getZipcode() { return zipcode; }
    public void setZipcode(long zipcode) { this.zipcode = zipcode; }
}

现在使用以下命令编译此代码:javac -d bin src / base / package / * .java.这会将位于src中的所有源文件编译到bin目录:

base-dir
|- src
|  \- base
|     \- package
|        |- Employee.java
|        \- Address.java
\- bin
   \- base
      \- package
         |- Employee.class
         \- Address.class

要为编译的类获取正确的Jar-archive,请使用:jar -cf test.jar -C bin /.这将生成一个test.jar存档,其中包含以下内容:

test.jar
|- base
|  \- package
|     |- Employee.class
|     \- Address.class
\- META-INF
   \- MANIFEST.MF

您现在可以删除bin目录及其所有内容,因为我们需要的所有文件都存储在存档中,并证明模式实际上是从Jar存档中的文件生成的.

在最终完成所有准备工作后,可以解决实际问题 – 如何从位于该test.jar存档中的.class文件生成模式:

只需运行以下命令:schemagen -cp test.jar base.package.Employee,它应生成类似于以下代码段的架构定义:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="employee" type="employee"/>

  <xs:complexType name="employee">
    <xs:sequence>
      <xs:element name="address" type="address" minOccurs="0"/>
      <xs:element name="designation" type="xs:string" minOccurs="0"/>
      <xs:element name="salary" type="xs:double"/>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:int" use="required"/>
  </xs:complexType>

  <xs:complexType name="address">
    <xs:sequence>
      <xs:element name="city" type="xs:string" minOccurs="0"/>
      <xs:element name="line1" type="xs:string" minOccurs="0"/>
      <xs:element name="line2" type="xs:string" minOccurs="0"/>
      <xs:element name="state" type="xs:string" minOccurs="0"/>
      <xs:element name="zipcode" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

希望这很简单,可以遵循

编辑:似乎jaxb2-maven-plugin以及ant-task根本不能使用类文件,所以最简单和最简单的解决方案可能是提供一个脚本文件(Windows上的.bat ;. sh on * nix / Mac)你只需手动调用命令:

由于我目前在Windows 7上运行,自动生成模式到项目的schemas子目录中的脚本如下所示:

schemagen -cp path/to/jar/*.jar -d ./schemas/ package.ClassName

然后,您可以使用绑定到generate-sources阶段的maven exec-plugin简单地调用该脚本(我已将其放置在项目的scripts子目录中):

<plugin>
    <artifactId>exec-maven-plugin</artifactId>
    <groupId>org.codehaus.mojo</groupId>
    <executions>
        <execution>
            <id>Generate schemas from class files contained in a jar</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>${basedir}/scripts/generate-sources.bat</executable>
            </configuration>
        </execution>
    </executions>
</plugin>

然后,应该在执行mvn generate-sources或任何后续阶段maven提供时自动生成模式.

编辑:我已经稍微修改了脚本,因为它可以处理通配符,虽然你必须指定* .jar而不是* – 但我想这应该足够好,至少这可以节省您手动输入名称包含JAXB类的Jar文件

标签:java,xsd,mojo,jaxb2,schemagen
来源: https://codeday.me/bug/20190703/1366318.html