在Intellij的Maven项目中设置picocli注解处理器与模块。

huangapple go评论72阅读模式
英文:

Set up picocli Annotation Processor in Intellij maven project with modules

问题

我无法理解如何在基于Maven的IntelliJ项目中设置picocli注解处理器。

考虑以下主POM:

<groupId>my.group.id</groupId>
<artifactId>example</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<modules>
    <module>core</module>
    <module>util</module>
</modules>

<dependencies>
    <dependency>
        <groupId>info.picocli</groupId>
        <artifactId>picocli</artifactId>
        <version>4.5.1</version>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>info.picocli</groupId>
                        <artifactId>picocli-codegen</artifactId>
                        <version>4.5.1</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <arg>-Aproject=${project.groupId}/${project.artifactId}</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

picocli代码位于核心模块。但是,一旦我在编译器插件中添加配置,所有我的IntelliJ配置都变得无效。无论我是将pico配置放在主模块还是核心模块,两者都不起作用。核心模块的POM仅用于测试和打包。

代码仍然可以编译并且可用作jar(没有编译时错误检查),但在IntelliJ中无法使用。示例可以工作,但它不使用模块。

我应该怎么在IntelliJ中修复这个问题?

英文:

I'm can't figure out how to setup picocli Anotation processor in a maven based intellij project.

Considere the following main pom:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;groupId&gt;my.group.id&lt;/groupId&gt;
&lt;artifactId&gt;example&lt;/artifactId&gt;
&lt;version&gt;1.0&lt;/version&gt;
&lt;packaging&gt;pom&lt;/packaging&gt;

&lt;modules&gt;
        &lt;module&gt;core&lt;/module&gt;
		&lt;module&gt;util&lt;/module&gt;
&lt;/modules&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;info.picocli&lt;/groupId&gt;
        &lt;artifactId&gt;picocli&lt;/artifactId&gt;
        &lt;version&gt;4.5.1&lt;/version&gt;
    &lt;/dependency&gt;
	&lt;!-- Other dependencies --&gt;
&lt;/dependencies&gt;

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
            &lt;configuration&gt;
      &lt;annotationProcessorPaths&gt;
        &lt;path&gt;
          &lt;groupId&gt;info.picocli&lt;/groupId&gt;
          &lt;artifactId&gt;picocli-codegen&lt;/artifactId&gt;
          &lt;version&gt;4.5.1&lt;/version&gt;
        &lt;/path&gt;
      &lt;/annotationProcessorPaths&gt;
      &lt;compilerArgs&gt;
        &lt;arg&gt;-Aproject=${project.groupId}/${project.artifactId}&lt;/arg&gt;
      &lt;/compilerArgs&gt;
    &lt;/configuration&gt;
        &lt;/plugin&gt;
&lt;/build&gt;

</project>

The picocli code is in the core module. But as soon as I add the configuration in the complier plugin all my IntlliJ configuration become invalid. It doesn't matter if I put the pico config in the main module or the core, both don't work. The core module pom is just for testing and packaging.

The code still compiles and is usable as an jar (No compile time err checking), but not in IntelliJ.
The example works. But it doesn't use modules.

What can I do to fix it in intelliJ?

答案1

得分: 1

这似乎是IntelliJ在处理非标准Maven项目时出现的问题。

遵循标准Java规则的Maven项目:

$PROJECT
│  pom.xml
│  ReadMe.md
├─core
│  │  pom.xml
│  └─src
│      └─main
│          └─java
│              └─your
│                  └─package
│                      └──Commons_cliTest.java
│                    
└─util
	│  pom.xml
	└─src
		└─main
			└─java
				└─your
					└─package
                       └─YourClasses.java

这可以完美地运行。您只需要在子模块pom中设置picocli配置。

例如,将以下内容放入core pom.xml中

    &lt;plugin&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;configuration&gt;
            &lt;annotationProcessorPaths&gt;
                &lt;path&gt;
                    &lt;groupId&gt;info.picocli&lt;/groupId&gt;
                    &lt;artifactId&gt;picocli-codegen&lt;/artifactId&gt;
                    &lt;version&gt;4.5.1&lt;/version&gt;
               &lt;/path&gt;
            &lt;/annotationProcessorPaths&gt;
            &lt;compilerArgs&gt;
                &lt;arg&gt;-Aproject=${project.groupId}/${project.artifactId}&lt;/arg&gt;
            &lt;/compilerArgs&gt;
        &lt;/configuration&gt;
    &lt;/plugin&gt;

因此,问题与IntelliJ有关。感谢Remko Popma的帮助来解决这个问题。

英文:

This seams to be an issue with IntelliJ not properly handling non standard maven projects.

A maven project that follows standard java rules:

$PROJECT
│  pom.xml
│  ReadMe.md
├─core
│  │  pom.xml
│  └─src
│      └─main
│          └─java
│              └─your
│                  └─package
│                      └──Commons_cliTest.java
│                    
└─util
	│  pom.xml
	└─src
		└─main
			└─java
				└─your
					└─package
                       └─YourClasses.java

This works perfectly fine. You just need set the picocli config in the child module pom.

For example, put this in the core pom.xml

    &lt;plugin&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;configuration&gt;
            &lt;annotationProcessorPaths&gt;
                &lt;path&gt;
                    &lt;groupId&gt;info.picocli&lt;/groupId&gt;
                    &lt;artifactId&gt;picocli-codegen&lt;/artifactId&gt;
                    &lt;version&gt;4.5.1&lt;/version&gt;
               &lt;/path&gt;
            &lt;/annotationProcessorPaths&gt;
            &lt;compilerArgs&gt;
                &lt;arg&gt;-Aproject=${project.groupId}/${project.artifactId}&lt;/arg&gt;
            &lt;/compilerArgs&gt;
        &lt;/configuration&gt;
    &lt;/plugin&gt;

So the problem is IntelliJ related.
Thanks for the help form Remko Popma to figure this out

huangapple
  • 本文由 发表于 2020年10月5日 16:30:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64205037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定