0

I have two different profiles (dev and prod) defined in my pom.xml. I don't want to include an embedded server while building a project with a prod profile. I know even if I don't exclude embedded server from the jar, I can deploy it on other servers.

I have checked how two exclude tomcat using below snippet:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

I'm not able to figure out how to exclude it on the basis of the selected profile. Below are the build and profile properties of my POM.xml. Please guide.

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.properties</include>
                    <include>application-${profileName}.properties</include>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build>
<profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profileName>dev</profileName>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profileName>prod</profileName>
            </properties>
        </profile>
</profiles>

Thanks in advance.

Joao Jorge
  • 103
  • 1
  • 2
  • 16
Pawan Kumar
  • 259
  • 1
  • 6
  • 17
  • I don't understand why are you using maven profiles instead of Spring Boot profiles for your purposes...and if you have the need to produce one with embedded server and one without you should make two different artifacts of it and also that lets handle different dependencies very clean. I strongly discourage to use profile with different dependencies..which will result in issues if you like to build releases... – khmarbaise Dec 28 '19 at 11:39
  • @khmarbaise I used maven profile because I wanted to exclude application.properties based on the profile during the packaging. I believe spring profiles and maven profiles are different things. Correct me if I am wrong. – Pawan Kumar Dec 28 '19 at 13:01

2 Answers2

1

Try to use dependencies tag into your profile , include only the dependencies that you want for that profile.

<profiles>
    <profile>
        <id>dev</id>
        <dependencies>
            // do stuff
        </dependencies>
    </profile>
</profiles>
pioardi
  • 181
  • 2
  • 7
1

Try something like that:

    <!-- Other profiles -->

    <profile>
        <id>prod</id>
        <dependencies>
            <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-tomcat</artifactId>
              <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>

    <!-- Other profiles -->

</profiles>

However, you have to be careful to the application packaging. Maybe you need to switch the fat jar (used for the development environment) to a war for your production environment (Since you will exclude the embedded server).

Katy
  • 1,023
  • 7
  • 19