如何配置”Maven”插件的属性在”properties”部分。

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

How to configure the properties of the maven plug-ins in the "properties" section

问题

配置在jar和assembly插件中声明的主类,我必须像以下示例中所示,在插件声明中打开配置标签:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.example.Main</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

要进行类似的操作来配置构建插件使用的Java版本:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>11</source>
            <target>11</target>
        </configuration>
    </plugin>

但通常为了使pom.xml更易于阅读,我会避免在插件部分中使用这些配置标签,而是在属性部分中使用以下属性来配置Java编译器插件使用的Java版本:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

是否可能以类似的方式配置由jar和assembly插件生成的清单文件中声明的主类?

在查阅关于jar插件的旧Maven文档时:
https://maven.apache.org/archives/maven-1.x/plugins/jar/properties.html

提到了属性“maven.jar.mainclass”,我相信在某个时候可以通过项目属性部分定义清单文件的主类,如下所示:

    <properties>
        <maven.jar.mainclass>com.example.Main</maven.jar.mainclass>
    </properties>

但是,当我尝试以这种方式运行生成的jar文件时,会收到错误消息“no main manifest attribute, in example.jar”。因此,是否有任何方法可以在不直接打开插件部分的配置标签的情况下配置主类呢?

英文:

To configure the main class declared in the manifest file for the jar and assembly plugins I have to open configuration tags in the plugin declaration as in the example below:

    &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.2.0&lt;/version&gt;
        &lt;configuration&gt;
            &lt;archive&gt;
                &lt;manifest&gt;
                    &lt;mainClass&gt;com.example.Main&lt;/mainClass&gt;
                &lt;/manifest&gt;
            &lt;/archive&gt;
        &lt;/configuration&gt;
    &lt;/plugin&gt;

I would have to do something similar to configure the version of java used by the build plugin:

    &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;configuration&gt;
            &lt;source&gt;11&lt;/source&gt;
            &lt;target&gt;11&lt;/target&gt;
        &lt;/configuration&gt;
    &lt;/plugin&gt;

But, normally, to avoid these configuration tags in the plugin section to make pom.xml easier and more pleasant to read, I configure the version of java used in the java compiler plugin with the following properties in the properties section:

    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;

Would it be possible to configure the main class declared in the manifest file generated by the jar and assembly plug-ins in the same way?

Checking this old maven documentation about the jar plugin:
https://maven.apache.org/archives/maven-1.x/plugins/jar/properties.html

The property "maven.jar.mainclass" is mentioned, I believe that at some point it was possible to define the main class of the manifest file by the project properties section as follows:

    &lt;properties&gt;
        &lt;maven.jar.mainclass&gt;com.example.Main&lt;/maven.jar.mainclass&gt;
    &lt;/properties&gt;

But when I try to run the jar generated in this way, I get the error message "no main manifest attribute, in example.jar".
So is there any way to configure the main class without having to open those configuration tags directly in the plugins section?

答案1

得分: 0

<properties>
    <maven.jar.mainclass>com.example.Main</maven.jar.mainclass>
</properties>
...

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>${maven.jar.mainclass}</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

You can always use your own placeholders anywhere and specify them as a property. Just be aware that now the value is not a constant anymore, but became a variable. In other words, you can now use 'mvn package -Dmaven.jar.mainclass=path.to.nonexstingClass'. It is powerful, but in this case, your jar has become useless because of the non-existing class, and such issue is almost impossible to track back.
英文:
&lt;properties&gt;
    &lt;maven.jar.mainclass&gt;com.example.Main&lt;/maven.jar.mainclass&gt;
&lt;/properties&gt;

...

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.2.0&lt;/version&gt;
    &lt;configuration&gt;
        &lt;archive&gt;
            &lt;manifest&gt;
                &lt;mainClass&gt;${maven.jar.mainclass}&lt;/mainClass&gt;
            &lt;/manifest&gt;
        &lt;/archive&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

You can always use your own placeholders anywhere and specify them as a property. Just be aware that now the value is not a constant anymore, but became a variable. In other words, you can now use mvn package -Dmaven.jar.mainclass=path.to.nonexstingClass. It is powerfull, but in this case your jar has become useless because of the non-existing class, and such issue is almost impossible to track back.

huangapple
  • 本文由 发表于 2020年9月3日 22:55:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63726268.html
匿名

发表评论

匿名网友

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

确定