【问题标题】:How to minimize maven pom.xml如何最小化maven pom.xml
【发布时间】:2017-10-16 16:16:14
【问题描述】:

我正在开发一个 spring-boot maven 项目(Maven 3.5、Java 8)。因为在我的项目中使用 Swagger Codegen 生成类,所以 pom.xml 的“plugins”标签越来越大:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin> 
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.2.3</version>
            <executions>
                <execution>
                    <id>file1</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>src/main/resources/swagger/../file1.json</inputSpec>
                        <generateApis>false</generateApis>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <generateApiDocumentation>false</generateApiDocumentation>
                        <modelPackage>com.bla.bla.model</modelPackage>
                        <templateDirectory>src/main/resources/swagger/templates</templateDirectory>
                        <language>spring</language>
                        <modelNamePrefix>ABC</modelNamePrefix>
                        <configOptions>
                            <interfaceOnly>true</interfaceOnly>
                            <java8>true</java8>
                        </configOptions>
                    </configuration>
                </execution>
                <execution>
                    <id>file2</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>src/main/resources/swagger/.../file2.json</inputSpec>
                        <generateApis>false</generateApis>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <generateApiDocumentation>false</generateApiDocumentation>
                        <modelPackage>com.bla.bla.model</modelPackage>
                        <templateDirectory>src/main/resources/swagger/templates</templateDirectory>
                        <language>spring</language>
                        <modelNamePrefix>ABC</modelNamePrefix>
                        <configOptions>
                            <interfaceOnly>true</interfaceOnly>
                            <java8>true</java8>
                        </configOptions>
                    </configuration>
                </execution>
...

如果我将一些插件移动到另一个 xml 文件中,是否可以将此 xml 文件包含到 pom.xml 中?如果不可能,那我该如何最小化这个文件?

编辑:这个问题不是Is it possible to split maven pom files? 的重复问题。我的项目还没有很大。只有 pom.xml 文件越来越大,因此没有必要将我的代码分成模块并以具有父子 pom.xml 文件的方式组织它。我需要一些不同的东西。

【问题讨论】:

标签: xml maven swagger swagger-codegen


【解决方案1】:

您可以在&lt;pluginManagement&gt; 元素中定义公共插件属性一次,然后为每次执行定义特定配置。即&lt;inputSpec&gt;src/main/resources/swagger/../file1.json&lt;/inputSpec&gt;

或者您可以在另一个父项目中执行相同的操作 (&lt;pluginManagment&gt;),并且在所有子项目中只放置特定的配置。

更新:

示例:

...
<build>
  <pluginManagement>
    <plugins>
        <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.2.3</version>
            <configuration>
                <generateApis>false</generateApis>
                <generateSupportingFiles>false</generateSupportingFiles>
                <generateApiDocumentation>false</generateApiDocumentation>
                <modelPackage>com.bla.bla.model</modelPackage>
                <templateDirectory>src/main/resources/swagger/templates
                </templateDirectory>
                <language>spring</language>
                <modelNamePrefix>ABC</modelNamePrefix>
                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <java8>true</java8>
                </configOptions>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>
<plugins>
    <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>file1</id>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>src/main/resources/swagger/../file1.json</inputSpec>
                </configuration>
            </execution>
            <execution>
                <id>file2</id>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>src/main/resources/swagger/.../file2.json</inputSpec>
                </configuration>
            </execution>
        </executions>
    </plugin>
  </plugins>
</build>             

...

【讨论】:

  • 我的项目还不算大。只有 pom.xml 文件越来越大,因此没有必要将我的代码分成模块并以具有父子 pom.xml 文件的方式组织它。我想我必须使用模块和父子 pom.xml 层次结构来实现您的建议,对吧?
  • 没有必要。您可以在同一 pom.xml 中定义 &lt;pluginManagement&gt; 中的公共属性一次,然后在您实际的 &lt;plugin&gt; &lt;execution&gt;&lt;configuration&gt; 中仅设置特定属性。
  • 类似于“相同 pom”配置的添加示例。
  • 非常感谢。我在互联网上找不到这样的例子。我编辑了 pom.xml,现在它可以工作了!
  • 不客气。请记住 - Maven 配置是从&lt;parent&gt; 分层的,即您可以拥有具有非常常见配置的最终父级,然后是具有更具体配置的另一组父级,依此类推直至实际项目。所有配置都被继承,并且也被下一级重载。它与&lt;modules&gt; 层次结构无关。 &lt;modules&gt; 定义了要一起构建的其他项目。特定模块可以具有定义不同配置的不同父级。很多时候两个层次结构一起使用,但没有必要。
猜你喜欢
  • 2014-01-09
  • 2011-05-10
  • 2011-03-03
  • 2012-04-19
  • 2017-06-26
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 2012-10-05
相关资源
最近更新 更多