Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a maven dependency provided based on active spring profile

So I am building a spring boot web application, packaging as a war, and deploying to a tomcat application server.

I have the following dependency in my pom.xml for tomcat:

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

The scope of this dependency needs to be provided in order to be able to deploy it to a tomcat instance. However, when I want to run the war via the spring boot CLI or via IntelliJ's default spring boot run configuration, I need to remove the <scope>provided</scope> in order for it to run the embedded tomcat.

My question is, is there some way to make the dependency conditionally provided based on an active spring profile, or some other method?

like image 399
Andrew Mairose Avatar asked Feb 17 '17 13:02

Andrew Mairose


People also ask

Is it possible to add library dependencies inside a profile in Maven?

Within each profile element, we can configure many elements such as dependencies, plugins, resources, finalName. So, for the example above, we could add plugins and their dependencies separately for integration-tests and mutation-tests.


2 Answers

You can't control dependencies via spring profiles. However you can control spring profiles by maven profiles and it can solve your problem.

You can declare several maven profiles in your application and provide different set of dependencies for each of them. Then you can configure maven profiles to use particular spring profile. Take a look on maven profiles and an example of such configuration in this thread

like image 128
Sasha Shpota Avatar answered Nov 11 '22 01:11

Sasha Shpota


In your specific case, you can just do : In the dependencies to run spring-boot with embedded tomcat :

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

And in a profile to deploy under tomcat

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

After, to build for a specific profile

mvn clean install -Ptomcat
like image 30
Claude Michiels Avatar answered Nov 11 '22 02:11

Claude Michiels