使用 exec-maven-plugin 到 maven 阶段

Posted

技术标签:

【中文标题】使用 exec-maven-plugin 到 maven 阶段【英文标题】:Using exec-maven-plugin to a maven phase 【发布时间】:2018-07-09 05:26:46 【问题描述】:

我正在使用 exec-maven-plugin 执行 java 应用程序以在我的项目中生成一些代码:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.codegenerator.CodeGeneratorApplication</mainClass>
            </configuration>
        </plugin>

如果我在命令行“mvn exec:java”中执行,这工作正常,但现在我想将此代码生成附加到 maven 阶段“generate-resources”,所以我将 pom 更改为:

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>codegeneration</id>
                    <phase>generate-resources</phase>
                    <goals><goal>java</goal></goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.codegenerator.CodeGeneratorApplication</mainClass>
            </configuration>
        </plugin>

但是当我突然执行“mvn exec:java”时,我得到了如下错误:

[09:52:03.926] [] [] [com.codegenerator.CodeGeneratorApplication.main()] [WARN ] [AbstractApplicationContext.java:550] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'codeGeneratorApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'cnp.application.acronym' in value "$cnp.application.acronym"
[09:52:03.940] [] [] [com.codegenerator.CodeGeneratorApplication.main()] [INFO ] [DirectJDKLog.java:179] - Stopping service Tomcat
[09:52:03.982] [] [] [com.codegenerator.CodeGeneratorApplication.main()] [INFO ] [AutoConfigurationReportLoggingInitializer.java:101] - 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
[09:52:03.987] [] [] [com.codegenerator.CodeGeneratorApplication.main()] [ERROR] [SpringApplication.java:815] - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'codeGeneratorApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'cnp.application.acronym' in value "$cnp.application.acronym"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at com.codegenerator.CodeGeneratorApplication.main(CodeGeneratorApplication.java:26) 

有人可以帮我解决什么问题吗?谢谢!

【问题讨论】:

【参考方案1】:

您的“配置”元素需要位于“执行”元素内。

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>codegeneration</id>
                <phase>generate-resources</phase>
                <goals><goal>java</goal></goals>
                <configuration>
                   <mainClass>com.codegenerator.CodeGeneratorApplication</mainClass>
                </configuration>
            </execution>
        </executions>

</plugin>

【讨论】:

以上是关于使用 exec-maven-plugin 到 maven 阶段的主要内容,如果未能解决你的问题,请参考以下文章

exec-maven-plugin 生成的进程会阻塞 maven 进程

exec-maven-plugin 给出类未找到异常

使用 exec-maven-plugin 运行守护程序,避免 `IllegalThreadStateException`

用 exec.mainClass 覆盖 exec-maven-plugin

执行 exec-maven-plugin 不是正常构建过程的一部分,并且没有继承

未能执行目标 org.codehaus.mojo:exec-maven-plugin:1.6.0:java