Lucas Fernando
Lucas Fernando

Reputation: 353

How to define a profile in gradle like maven to replace properties

In my project, i use a profile that changes a property defined in a .pom file. That property is replaced on a file inside of a project. I want to migrate to gradle but i didn't know how to fully convert.

My question is, how to convert this to gradle?

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${maven-jar-plugin.version}</version>

                <configuration>
                    <archive>
                        <index>true</index>
                        <manifestEntries>
                            <Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
                            <Implementation-Title>${project.artifactId}</Implementation-Title>
                            <Implementation-Version>${project.version}</Implementation-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <key.test>jdbc/test</key.test>
    </properties>

    <profiles>
        <profile>
            <id>wildfly</id>
            <properties>
                <key.test>java:/jdbc/test</key.test>
            </properties>
        </profile>
    </profiles>

</project>

On my project, there is a file with a content like this:

<jta-data-source>${key.test}</jta-data-source>

Thank you

Upvotes: 2

Views: 6890

Answers (1)

PapaStan
PapaStan

Reputation: 247

Maven-like profiles with gradle :

Create build files for each Maven profile you had in the project root: profile-default.gradle and profile-wildfly.gradle. Then declare your key.test property in those files.

Next, add a condition similar to the following to the main build file:

if (!hasProperty('buildProfile')) ext.buildProfile = 'default'
apply from: "profile-${buildProfile}.gradle"

When you want to build with your wildfly profile call :

gradle -PbuildProfile=wildfly build

Source

Resource filtering with gradle :

Use the ReplaceTokens filter (from ant) within your build.gradle file:

import org.apache.tools.ant.filters.*

processResources {
    filter ReplaceTokens, tokens: [
        "application.version": project.property("application.version")
    ]
}

Then use of @ instead of ${} as token identifier.

<jta-data-source>@key.test@</jta-data-source>

Source

Upvotes: 4

Related Questions