pixel
pixel

Reputation: 10537

IntelliJ - modifying <groupId> in maven pom.xml to contain a variable shows "Properties in parent definition are prohibited"

I have same issue and I am on

I have project that consists of multiple subprojects like:

myApp
|- myApp.war
|- myApp.ear
|- myApp.service

In root (myApp) pom.xml, I have properties section like:

<environment>env1</environment>

Also in myapp root, I set groupId like below and pom.xml will not show red squiggly underline, so all good here:

<groupId>com.example.projName-${env}</groupId>  //this is fine

I can do same for my subproject artifacts myApp.war, myApp.ear, myApp.service, so all good here as well.

However, all subgrup projects have to reference parent like:

<parent>
    <groupId>com.example.projName-${environment}</groupId>  //get red squiggly underline
    <artifactId>myApp</artifactId>      
    <version>1.0.0</version>
</parent>

Modifying the groupId in <parent> element, results in red squiggly underline and when I hover over it, it says Properties in parent definition are prohibited.

The reason I modify groupId this way is because I want all my projects' modules to be pushed to Nexus in common location whose name is defined by projName-staging, projName-qa etc.

However, I can still build my application doing mvn clean install and deploy to Nexus snapshot with mvn deploy.

But attempt to deploy to Nexus releases fails on mvn release:prepare stage with following:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:3.0.1:prepare (default-cli) on project myApp: Can't release project due to non released dependencies :
[ERROR]     com.example.myapp-${environment}:myapp:pom:1.0.1-SNAPSHOT
[ERROR] in project 'myapp-war' (com.example.myapp-env1:myapp-war:war:1.0.1-SNAPSHOT)

Upvotes: 0

Views: 88

Answers (1)

kapandron
kapandron

Reputation: 3671

Use Maven profiles for environment-specific builds. Instead of setting groupId with an environment property try using profiles for environment-specific configurations. Define profiles in your root pom.xml like so:

<profiles>
    <profile>
        <id>env1</id>
        <properties>
            <group.name.suffix>-env1</group.name.suffix>
        </properties>
    </profile>
    <profile>
        <id>qa</id>
        <properties>
            <group.name.suffix>-qa</group.name.suffix>
        </properties>
    </profile>
</profiles>

Then modify your groupId to include the suffix:

<groupId>com.example.projName${group.name.suffix}</groupId>

You can activate a profile during the build process for specific environments with mvn clean install -Penv1 or -Pqa, allowing each deployment to have its unique Nexus path based on the groupId suffix.

By defining profiles in your pom.xml for each environment (e.g. env1, qa), you can dynamically set the suffix for groupId, ensuring that your builds and deployments to Nexus can reference the correct environment without modifying the parent groupId.

Upvotes: 1

Related Questions