英文:
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:
<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>
I would have to do something similar to configure the version of java used by the build plugin:
<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>
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:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
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:
<properties>
<maven.jar.mainclass>com.example.Main</maven.jar.mainclass>
</properties>
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.
英文:
<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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论