You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Maven pom.xml中转换artifactId属性后设置mainClass值?

Maven中实现artifactId转大驼峰作为mainClass值

可以实现,下面提供两种常用方案:

方案一:借助Properties Maven Plugin + Groovy表达式

通过properties-maven-plugin在构建初始化阶段,用Groovy脚本将短横线分隔的artifactId转换为大驼峰格式,生成新属性后在mainClass中引用。

在pom.xml的<build><plugins>中添加以下配置:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.1.0</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>set-system-properties</goal>
            </goals>
            <configuration>
                <properties>
                    <property>
                        <name>convertedArtifactId</name>
                        <value>
                            ${groovy:
                                def parts = project.artifactId.split('-');
                                parts.collect { it.capitalize() }.join('')
                            }
                        </value>
                    </property>
                </properties>
            </configuration>
        </execution>
    </executions>
</plugin>

之后在配置mainClass的插件(比如maven-jar-plugin)中引用新属性:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <archive>
            <manifest>
                <!-- 替换为你的实际包路径 -->
                <mainClass>com.example.${convertedArtifactId}</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

方案二:借助Maven Antrun Plugin + JavaScript脚本

用maven-antrun-plugin执行Ant脚本,通过JavaScript处理字符串转换逻辑:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <property name="rawArtifactId" value="${artifactId}"/>
                    <script language="javascript">
                        var parts = project.getProperty("rawArtifactId").split("-");
                        var camelCaseId = "";
                        for (var i = 0; i < parts.length; i++) {
                            camelCaseId += parts[i].charAt(0).toUpperCase() + parts[i].slice(1);
                        }
                        project.setProperty("convertedArtifactId", camelCaseId);
                    </script>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

同样在mainClass配置中引用${convertedArtifactId}即可。

两种方案的核心都是先生成转换后的属性,再在mainClass中复用该属性,确保构建过程中自动完成格式转换。

内容的提问来源于stack exchange,提问作者Alexander Lopatin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 04:55:32