Skip maven dependencies based on a flag

43 Views Asked by At

I am trying to set up a Parent pom for my services to easily manage dependencies. I have my parent pom like this,

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.12</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.myapp</groupId>
    <artifactId>myapp-common-parent-bom</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>
    <name>myapp-common-parent-bom</name>

    <properties>
        <springboot.version>2.7.12</springboot.version>
        <spring-cloud.version>2022.0.4</spring-cloud.version> 
        <swagger.version>1.6.3</swagger.version>
        <sonar.language>java</sonar.language>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${springboot.version}</version>
        </dependency>
    </dependencies>
</project>

And, I am using this as the parent in other services. Sample here,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://maven.apache.org/POM/4.0.0"
    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>
    <parent>
        <groupId>com.myapp</groupId>
        <artifactId>myapp-common-parent-bom</artifactId>
        <version>0.0.1</version>
    </parent>
    <groupId>com.myapp</groupId>
    <artifactId>my-service</artifactId>
    <version>0.0.1</version>
    <name>my-service</name>
</project>

Now, I would like to control the dependencies that I need to install in my service using a flag. Something like this <useLombok>true</useLombok>. How can I do this?

I tried this in the parent pom, but it din't work.

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>!${useLombok}</optional>
            <version>1.18.20</version>
        </dependency>
    </dependencies>

So, I am looking for help how I can achieve this.

0

There are 0 best solutions below