4

Maven includes the contents of the file from one module to another

 3 years ago
source link: https://www.codesd.com/item/maven-includes-the-contents-of-the-file-from-one-module-to-another.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Maven includes the contents of the file from one module to another

advertisements

I have a maven application that looks like this

application_name/
    module1
        src/main/resources
            file_snippet.xml
    module2
        src/main/resources
            file_snippet.xml
    module3
        src/main/resources
            file.xml

file.xml should be like this

<workflow>
  <action>
  <%= module1/src/main/resources/file_snippet.xml %>
  </action>

  <action>
  <%= module2/src/main/resources/file_snippet.xml %>
  </action>

</workflow>

I want to include the contents of file_snippet.xml from module2 and module2 into file.xml of module3 before the build. Is that possible in maven? Is there some sort of templating language or plugin I can use?


This is not easy, there are two parts you need to implement.

  1. get the snippets from the other module
  2. assemble the xml file using the include

for 1. you can use the dependency:unpack mojo:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>unpack</id>
          <phase>initialize</phase>
          <goals>
            <goal>unpack</goal>
          </goals>
          <configuration>
            <outputDirectory>${project.build.directory}/snippets</outputDirectory>
            <includes>snippets/*.xml</includes>
            <artifactItems>
              <artifactItem>
                <groupId>your.app</groupId>
                <artifactId>module1</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
              </artifactItem>
              <artifactItem>
                <groupId>your.app</groupId>
                <artifactId>module2</artifactId>
                <version>${project.version}</version>
                <type>jar</type>
              </artifactItem>
            </artifactItems>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Now you have copied all snippets from module1.jar/snippets and module2.jar/snippets to target/snippets (this was the easier part).

For 2. you need to pick a template engine and create a main class to assemble your template with it, probably using the exec:java mojo something like this:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <mainClass>com.yourcompany.YourTemplateParser</mainClass>
      <arguments>
        <argument>path/to/your/template</argument>
        <argument>path/to/the/snippets/folder</argument>
        <argument>target/path</argument>
      </arguments>
    </configuration>
  </plugin>

(Personally, I tend to use GMaven instead of exec:java, as you can write inline groovy scripts without creating custom java classes)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK