Distribute XSD files over multiple Maven Artifacts

前端 未结 1 919
滥情空心
滥情空心 2021-02-09 10:34

Here is a small Example of what I would like to achieve:

Maven Artifact A is one of many Webservices and defines a XSD Schema with definitions for Requests and Response

相关标签:
1条回答
  • 2021-02-09 11:19

    I think your question is reasonable. In the past I have often found that I have a Maven module that needs to do special processing on files that can be found in dependent JARs.

    What I have done in the past is to make Artifact B a dependency of Artifact A. Then in the pom.xml of Artifact A I use a special configuration of the Maven dependency plugin as follows:

    <plugin>
        <!-- Used to pull XSD files from the JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
              <execution>
                  <id>unpack-xsd-files</id>
                  <!-- Using the initialize phase because it is before the generate sources phase -->
                  <phase>initialize</phase>
                       <goals>
                           <goal>unpack</goal>
                       </goals>
                       <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <!-- Artifact that Holds our custom templates -->
                                    <groupId>com.mycorp</groupId>
                                    <artifactId>artifact-b</artifactId>
                                    <version>${artifact-b.version}</version>
                                    <type>jar</type>
                                </artifactItem>
                             </artifactItems>
                             <includes>**/*.xsd</includes>
                             <outputDirectory>${project.basedir}/target/xsd-includes</outputDirectory>
                       </configuration>
                </execution>
        </executions>
    </plugin>
    

    Now your XSD files of interest can be found in the target directory of Artifact A and from there you can do anything you want with them. You can include them as resources, reference them from other XSD files, embed them in your own JARs or whatever! As you can see from the configuration, you also don't have to put them in your target directory; you could put them directly into your /src/main/resources directory.

    You can add this configuration to any Maven module you want so that multiple modules can all work the same way. The nice thing about this approach is that it will also work from with Eclipse via M2Eclipse.

    0 讨论(0)
提交回复
热议问题