Hi am trying to switch the database based on the active profile, So I have used maven profile to filter the database dependency
Here is my pom file
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>couhbase</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>couhbase</spring.profiles.active>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-couchbase-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.couchbase.client</groupId>
<artifactId>java-client</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Here we have two profile when profile is couhbase it should connect couchbase DB, and if it is dev then it should connect to MongoDB
Here is my properties file
spring.couchbase.bootstrap-hosts=127.0.0.1
spring.couchbase.bucket.name=config
spring.couchbase.bucket.password=
spring.profiles.include=@spring.profiles.active@
and am setting the profile name while building project on maven build
so if the profile is couhbase am able to connect with couchbase without any issue, but when am trying to change profile to dev then am getting issue while building the project
here is the issue
Its trying to compile the ReactiveCouchbaseConfiguration class which extends AbstractReactiveCouchbaseConfiguration to set connection parameter, this is the class
@Configuration
@Profile("couhbase")
@EnableReactiveCouchbaseRepositories("com.baeldung.couchbase.domain.repository")
public class ReactiveCouchbaseConfiguration extends AbstractReactiveCouchbaseConfiguration {
private CouchbaseProperties couchbaseProperties;
@Value("${spring.profiles.active}")
private String activeProfile;
@Autowired
private Environment env;
public ReactiveCouchbaseConfiguration(CouchbaseProperties couchbaseProperties) {
//System.out.println( env.getActiveProfiles());
this.couchbaseProperties = couchbaseProperties;
}
@Override
protected List<String> getBootstrapHosts() {
return couchbaseProperties.getBootstrapHosts();
}
@Override
protected String getBucketName() {
return couchbaseProperties.getBucketName();
}
@Override
protected String getBucketPassword() {
return couchbaseProperties.getBucketPassword();
}
@Override
public CouchbaseEnvironment couchbaseEnvironment() {
return DefaultCouchbaseEnvironment
.builder()
.bootstrapHttpDirectPort(couchbaseProperties.getPort())
.build();
}
}
So am trying to avoid this class using profile annotation but it still compile this class Is there any way to avoid this class when we change profile from couhbase to dev?
Using @Profile just means that your bean will be ignored at runtime if the declared profiles don't match the active profiles. The class still has to be compiled though. Why are you trying to separate the dependencies in your build targets so hard? The ROI of that is negative, imho. You only save some MB on your artifact.
The easiest option is to just add the dependencies normally, without haggling them around in maven profiles. I'd recommend this one.
Another option would be to use springs autoconfiguration and @ConditionalOnClass. You could play around with this one.
The most complex option would be to move all the code (including the configuration bean) into a separate module/jar and add that to the corresponding maven profile too. Then, either use springs autoconfiguration options or profiles again to switch the configurations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.