使用Ivy解决XSD问题

时间:2010-06-28 12:07:51

标签: ant xsd ivy

请原谅双重帖子,但我很想回答这个问题。

我想就我采取的方法提出一些建议。我正在使用Ivy进行依赖管理,并且能够下载并使用我的所有jar文件没有问题。我还想在Ant中运行<schemavalidate>任务,并希望使用Ivy按照指定下载xsd和dtd,从而在初始下载后无需网络连接,也减少了构建时间。我想我有一个解决方案,但想要运行它 通过一些额外的眼睛进行健全检查和建议可能的改进。以下是我的构建脚本的相关部分。第一次检索调用使用我的默认ivysettings.xml,第二次调用使用专门用于检索xsd和dtd的设置文件。任何反馈都将不胜感激。

的build.xml:

<project etc>
    ...

    <target name="resolve" description="Retrieve dependencies with ivy">
        <ivy:retrieve refresh="true"
                  sync="true"
                  conf="compile,war,runtime,test,findbugs"
                  pattern="${ivy.lib.dir}/[conf]/[artifact]-[revision].[ext]"/>
        <ivy:settings id="xsd.settings" 
                      file="${search.server.home}/ivysettings-xsd.xml"/>
        <ivy:retrieve settingsref="xsd.settings"
                  refresh="false"
                  sync="false"
                  conf="xmlentities"
                  pattern="${ivy.lib.dir}/[conf]/[artifact].[ext]"/>
    </target>
    ...
</project>

的ivy.xml:

<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
    <dependencies>
        <!-- Jar files defined here but removed for brevity -->
        ...
        <dependency org="beans" name="spring-beans" rev="3.0" conf="xmlentities->default">
            <artifact name="spring-beans" type="xsd"/>
        </dependency>
        <dependency org="context" name="spring-context" rev="3.0" conf="xmlentities->default">
            <artifact name="spring-context" type="xsd"/>
        </dependency>
        <dependency org="mvc" name="spring-mvc" rev="3.0" conf="xmlentities->default">
            <artifact name="spring-mvc" type="xsd"/>
        </dependency>
        <dependency org="tool" name="spring-tool" rev="3.0" conf="xmlentities->default">
            <artifact name="spring-tool" type="xsd"/>
        </dependency>
        <dependency org="util" name="spring-util" rev="3.0" conf="xmlentities->default">
            <artifact name="spring-util" type="xsd"/>
        </dependency>
        <dependency org="javaee" name="javaee" rev="5" conf="xmlentities->default">
            <artifact name="javaee_5" type="xsd"/>
            <artifact name="web-app_2_5" type="xsd"/>
            <artifact name="javaee_web_services_client_1_2" type="xsd"/>
            <artifact name="jsp_2_1" type="xsd"/>
        </dependency>
        <dependency org="xmlschema" name="xmlschema" rev="2001" conf="xmlentities->default">
            <artifact name="XMLSchema" type="xsd"/>
            <artifact name="xml" type="xsd"/>
        </dependency>
    </dependencies>
</ivy-module>

ivysettings-xsd.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ivysettings>
    <settings defaultResolver="namespaces"/>
    <resolvers>
        <chain name="namespaces" returnFirst="true">
            <url name="w3-org-ns" checksums="">
                <artifact pattern="http://www.w3.org/2001/[artifact].[ext]"/>
            </url>
            <url name="javaee-ns" checksums="">
                <artifact pattern="http://java.sun.com/xml/ns/javaee/[artifact].[ext]"/>
            </url>
            <url name="spring-ns" checksums="">
                <artifact pattern="http://www.springframework.org/schema/[organisation]/[artifact].[ext]"/>
            </url>
        </chain>
    </resolvers>
</ivysettings>

1 个答案:

答案 0 :(得分:3)

有趣的问题。缓存Schema文件可以进行离线验证。

正如汤姆所说,我认为只需要一次检索。 (我的示例提取了jar和模式文件)

<ivy:retrieve pattern="${lib.dir}/[conf]/[artifact]-[revision].[ext]"/>

我对常春藤和设置文件进行了一些更改。

的ivy.xml

我使用ivy extra attributes来帮助生成Spring Schema URL:

<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
    <info organisation="com.myspotontheweb.demo" module="spring"/>

    <configurations defaultconfmapping="compile->default">
        <conf name="compile" description="Compile dependencies"/>
        <conf name="schemas" description="XML schema files"/>
    </configurations>

    <dependencies>
        <!-- Compile depedencies -->
        <dependency org="org.springframework" name="spring-core" rev="3.0.6.RELEASE"/>

        <!-- Schema dependencies -->
        <dependency org="org.springframework" name="schemas" rev="3.0" conf="schemas->default">
            <artifact name="spring-beans"   e:framework="beans" type="xsd"/>
            <artifact name="spring-context" e:framework="context" type="xsd"/>
            <artifact name="spring-mvc"     e:framework="mvc"   type="xsd"/>
            <artifact name="spring-tool"    e:framework="tool"  type="xsd"/>
            <artifact name="spring-util"    e:framework="util"  type="xsd"/>
        </dependency>

        <dependency org="com.sun.java" name="schemas" rev="5" conf="schemas->default">
            <artifact name="javaee_5"    type="xsd"/>
            <artifact name="web-app_2_5" type="xsd"/>
            <artifact name="javaee_web_services_client_1_2" type="xsd"/>
            <artifact name="jsp_2_1"     type="xsd"/>
        </dependency>

        <dependency org="org.w3" name="schemas" rev="2001" conf="schemas->default">
            <artifact name="XMLSchema" type="xsd"/>
            <artifact name="xml"       type="xsd"/>
        </dependency>
    </dependencies>
</ivy-module>

ivysettings.xml

默认情况下,将ivy配置为使用Maven存储库。使用modules声明将特殊架构模块路由到您的URL解析器。

<ivysettings>
    <settings defaultResolver="maven-repos"/>
    <resolvers>
        <chain name="maven-repos">
            <ibiblio name="central" m2compatible="true"/>
            ..
            Other Maven repositories go here
            ..
        </chain>
        <url name="spring-schemas">
            <artifact pattern="http://www.springframework.org/schema/[framework]/[artifact].[ext]"/>
        </url>
        <url name="javaee-schemas">
            <artifact pattern="http://java.sun.com/xml/ns/javaee/[artifact].[ext]"/>
        </url>
        <url name="w3-schemas">
            <artifact pattern="http://www.w3.org/2001/[artifact].[ext]"/>
        </url>
    </resolvers>
    <modules>
        <module organisation="org.springframework" name="schemas" resolver="spring-schemas"/>
        <module organisation="com.sun.java" name="schemas" resolver="javaee-schemas"/>
        <module organisation="org.w3"       name="schemas" resolver="w3-schemas"/>
    </modules>
</ivysettings>