跟着JHipster学做项目(3) - Maven的使用技巧(下)去掉因lifecycle引起的红叉

当我们在项目中引入swagger或者asciidoc等插件时,通常定义在generate-sources阶段执行,当使用eclipse时,pom.xml就会出现一个红叉,以swagger2markup-maven-project-template项目为例,克隆这个项目在本地,将项目引入到eclipse,

lifecycle

这个问题虽然不影响项目的编译以及运行,但是这个红叉不仅会引起视觉的不适,也实际会影响我们对项目健康状况的判断,下面我们通过对JHipster项目的pom.xml学习,看看如何解决这个问题。

原理是通过pluginManagement中action给定参数ignore,但是要注意如果简单把plugin放入pluginManagement会造成不执行,那么我们需要单独给出插件的执行配置,具体如下:

<build>
		<plugins>
			<!-- First, use the swagger2markup plugin to generate asciidoc -->
			<plugin>
				<groupId>io.github.swagger2markup</groupId>
				<artifactId>swagger2markup-maven-plugin</artifactId>
				<version>${swagger2markup.plugin.version}</version>
				<dependencies>
					<dependency>
						<groupId>io.github.swagger2markup</groupId>
						<artifactId>swagger2markup-import-files-ext</artifactId>
						<version>${swagger2markup.extension.version}</version>
					</dependency>
					<dependency>
						<groupId>io.github.swagger2markup</groupId>
						<artifactId>swagger2markup</artifactId>
						<version>${swagger2markup.version}</version>
					</dependency>
				</dependencies>
				<configuration>
					<swaggerInput>${swagger.input}</swaggerInput>
					<outputDir>${generated.asciidoc.directory}</outputDir>
					<config>
						<swagger2markup.markupLanguage>ASCIIDOC</swagger2markup.markupLanguage>
						<swagger2markup.pathsGroupedBy>TAGS</swagger2markup.pathsGroupedBy>
						<swagger2markup.extensions.dynamicOverview.contentPath>${project.basedir}/src/docs/asciidoc/extensions/overview</swagger2markup.extensions.dynamicOverview.contentPath>
						<swagger2markup.extensions.dynamicDefinitions.contentPath>${project.basedir}/src/docs/asciidoc/extensions/definitions</swagger2markup.extensions.dynamicDefinitions.contentPath>
						<swagger2markup.extensions.dynamicPaths.contentPath>${project.basedir}/src/docs/asciidoc/extensions/paths</swagger2markup.extensions.dynamicPaths.contentPath>
						<swagger2markup.extensions.dynamicSecurity.contentPath>${project.basedir}src/docs/asciidoc/extensions/security</swagger2markup.extensions.dynamicSecurity.contentPath>
					</config>
				</configuration>
				<executions>
					<execution>
						<phase>generate-sources</phase>
						<goals>
							<goal>convertSwagger2markup</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<!-- Run the generated asciidoc through Asciidoctor to generate other 
				documentation types, such as PDFs or HTML5 -->
			<plugin>
				<groupId>org.asciidoctor</groupId>
				<artifactId>asciidoctor-maven-plugin</artifactId>
				<version>1.5.5</version>
				<!-- Include Asciidoctor PDF for pdf generation -->
				<dependencies>
					<dependency>
						<groupId>org.asciidoctor</groupId>
						<artifactId>asciidoctorj-pdf</artifactId>
						<version>${asciidoctorj.pdf.version}</version>
					</dependency>
					<!-- Comment this section to use the default jruby artifact provided 
						by the plugin -->
					<dependency>
						<groupId>org.jruby</groupId>
						<artifactId>jruby-complete</artifactId>
						<version>${jruby.version}</version>
					</dependency>
					<!-- Comment this section to use the default AsciidoctorJ artifact provided 
						by the plugin -->
					<dependency>
						<groupId>org.asciidoctor</groupId>
						<artifactId>asciidoctorj</artifactId>
						<version>${asciidoctorj.version}</version>
					</dependency>
				</dependencies>
				<!-- Configure generic document generation settings -->
				<configuration>
					<sourceDirectory>${asciidoctor.input.directory}</sourceDirectory>
					<sourceDocumentName>index.adoc</sourceDocumentName>
					<attributes>
						<doctype>book</doctype>
						<toc>left</toc>
						<toclevels>3</toclevels>
						<numbered></numbered>
						<hardbreaks></hardbreaks>
						<sectlinks></sectlinks>
						<sectanchors></sectanchors>
						<generated>${generated.asciidoc.directory}</generated>
					</attributes>
				</configuration>
				<!-- Since each execution can only handle one backend, run separate executions 
					for each desired output type -->
				<executions>
					<execution>
						<id>output-html</id>
						<phase>generate-resources</phase>
						<goals>
							<goal>process-asciidoc</goal>
						</goals>
						<configuration>
							<backend>html5</backend>
							<outputDirectory>${asciidoctor.html.output.directory}</outputDirectory>
						</configuration>
					</execution>
					<execution>
						<id>output-pdf</id>
						<phase>generate-resources</phase>
						<goals>
							<goal>process-asciidoc</goal>
						</goals>
						<configuration>
							<backend>pdf</backend>
							<outputDirectory>${asciidoctor.pdf.output.directory}</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<pluginManagement>
			<plugins>
				<!-- This plugin's configuration is used to store Eclipse m2e settings 
					only. It has no influence on the Maven build itself. Remove when the m2e 
					plugin can correctly bind to Maven lifecycle -->
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>io.github.swagger2markup</groupId>
										<artifactId>swagger2markup-maven-plugin</artifactId>
										<versionRange>
											${swagger2markup.plugin.version}
										</versionRange>
										<goals>
											<goal>convertSwagger2markup</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore />
									</action>
								</pluginExecution>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>org.asciidoctor</groupId>
										<artifactId>asciidoctor-maven-plugin</artifactId>
										<versionRange>
											${asciidoctorj.version}
										</versionRange>
										<goals>
											<goal>process-asciidoc</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore />
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

这样配置后项目需要利用maven来update。

Good Luck,

Cheers.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章