1

我正在使用 maven 构建一个 jar,其中包含使用 jibx 从模式文件生成的代码。为此,我使用带有 schema-codegen 目标的 jibx-maven-plugin。我想将生成的 binding.xml 文件包含在生成的 maven jar 中。有什么方法可以指导 jar 创建以包含生成的 binding.xml

目前使用:

<plugin>
    <groupId>org.jibx</groupId>
    <artifactId>jibx-maven-plugin</artifactId>
    <version>1.2.3</version>
    <configuration>
        <schemaLocation>src/main/jibx</schemaLocation>
        <includeSchemas>
            <includeSchema>dataoneTypes.xsd</includeSchema>
        </includeSchemas>
        <options>
            <package>org.dataone.ns.service.types.v1</package>
        </options>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>schema-codegen</goal>
            </goals>
        </execution>

    </executions>
</plugin>
4

4 回答 4

1

大卫,

好的!虽然不需要包含 binding.xml 文件,但这是一种很好的做法。新的 jibx-maven-plugin 稍后可以在创建基于原始模式的新绑定时使用此文件。JiBX 源代码库中有大量示例。

由于 JiBX 启用了 OSGi,因此在创建 jar 文件时添加 OSGi 清单也是一个好习惯。这也简化了包含 binding.xml 文件的过程。即使您不使用 OSGi,您的 jar 也可以正常工作。这是您的项目文件的外观:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.dataone.ns.service</groupId>
    <artifactId>org.dataone.ns.service.types.v1</artifactId>
    <version>0.0.1</version>
    <packaging>bundle</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jibx</groupId>
                <artifactId>jibx-maven-plugin</artifactId>
                <version>1.2.3</version>

                <executions>
                    <execution>
                        <id>generate-java-code-from-schema</id>
                        <goals>
                            <goal>schema-codegen</goal>
                        </goals>
                        <configuration>
                            <schemaLocation>src/main/jibx</schemaLocation>
                            <includeSchemas>
                                <includeSchema>dataoneTypes.xsd</includeSchema>
                            </includeSchemas>
                            <options>
                                <package>org.dataone.ns.service.types.v1</package>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile-binding</id>
                        <goals>
                            <goal>bind</goal>
                        </goals>
                        <configuration>
                            <schemaBindingDirectory>target/generated-sources</schemaBindingDirectory>
                            <includes>
                                <include>binding.xml</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Include-Resource>META-INF/binding.xml=${basedir}/target/generated-sources/binding.xml</Include-Resource>
                        <Export-Package>org.dataone.ns.service.types.v1.*;version=${project.version}</Export-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-run</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-extras</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>

</project>

看看你的 jar 文件。您的类、binding.xml 文件和 OSGi 清单条目都在那里!

Don Corley jibx-maven-plugin 作者

于 2011-09-20T18:04:30.580 回答
0

您可以在您希望将其放置在 jar 中的目标目录中创建您的 binding.xml,如下所示:

...
<goals>
     <goal>schema-codegen</goal>
</goals>
<configuration>
...
     <targetDirectory>target/resources</targetDirectory>
...
</configuration>
...

绑定代码时,可以使用带<bindingDirectory>标签的引用这个目录

于 2011-09-06T13:09:29.830 回答
0

您可以使用build-helper-maven-plugin的add-resource目标来完成。

例子:

<build>
    <plugins>
        <plugin>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-maven-plugin</artifactId>
            <version>1.2.3</version>
            <executions>
                <execution>
                    <id>generate-java-code-from-schema</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>schema-codegen</goal>
                    </goals>
                    <configuration>
                        <schemaLocation>src/main/resources</schemaLocation>
                        <includeSchemas>
                            <includeSchema>foobar.xsd</includeSchema>
                        </includeSchemas>
                    </configuration>
                </execution>
                <execution>
                    <id>compile-binding</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>bind</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>${project.build.directory}/generated-sources</directory>
                                <includes>
                                    <include>binding.xml</include>
                                </includes>
                                <targetPath>JiBX</targetPath>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

您将在 jar 中找到 binding.xml 文件:

JiBX/binding.xml
于 2012-02-03T13:41:50.223 回答
0

您始终可以使用maven-antrun-plugin将您的文件(集)复制到目标/类。

确保:

  • 您将 jibx 插件附加到之前 package的阶段- 最好是generate-resources
  • 您将 antrun 执行附加到相同或以后,但同样,之前 package- 最好是generate-resourcesprocess-resources
  • jibx 插件声明在 antrun 声明之前

然后你可以使用这样的东西:

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
         <phase>generate-resources</phase>
         <goals>
           <goal>run</goal>
         </goals>
         <configuration>
           <tasks>
             <copy file="${project.build.directory}/PATH/TO/binding.xml" todir="${project.build.outputDirectory}/PATH/IN/JAR/"/>
           </tasks>
         </configuration>
       </execution>
     </executions>
</plugin>
...
于 2011-08-18T19:56:42.117 回答