账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    springboot从main方法运行后,pom中配置的profile无效
    35
    0

    在intellij idea中的springboot项目,通过运行application的main方法,启动springboot项目。

    但是在maven的pom.xml中,配置了profile,如果在运行时,将这些profile考虑进去。

    比如,我在pom中提供了两个profile,分别为activemq,和kafka,希望在不同的profile下,加载不同的消息依赖。

    但是如果直接在application中,启动main方法,总会因为当处于其中一个profile时,代码中关于另外一种消息的代码会因为找不到依赖而报错。

    下面这一段代码,是pom中配置了的两个profile.

    <profiles>
            <profile>
                <id>kafka</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <active.profile>kafka</active.profile>
                </properties>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.kafka</groupId>
                        <artifactId>spring-kafka</artifactId>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.kafka</groupId>
                        <artifactId>spring-kafka-test</artifactId>
                        <scope>test</scope>
                    </dependency>
                </dependencies>
                <build>
                    <resources>
                        <resource>
                            <directory>src/main/java/com/fw/sf/api/message/kafka</directory>
                            <excludes>
    
                            </excludes>
                        </resource>
                    </resources>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <configuration>
                                <excludes>
                                    <exclude>com/message/activemq/*.java</exclude>
                                </excludes>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
            <profile>
                <id>activemq</id>
                <properties>
                    <active.profile>activemq</active.profile>
                </properties>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-activemq</artifactId>
                        <exclusions>
                            <exclusion>
                                <groupId>org.springframework</groupId>
                                <artifactId>spring-jms</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.activemq</groupId>
                        <artifactId>activemq-pool</artifactId>
                    </dependency>
                </dependencies>
                <build>
                    <resources>
                        <resource>
                            <directory>src/main/java/com/fw/sf/api/message/kafka</directory>
                            <excludes>
                            </excludes>
                        </resource>
                    </resources>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <configuration>
                                <excludes>
                                    <exclude>com/message/kafka/*.java</exclude>
                                </excludes>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>

    因为添加了exclude,所以,使用mvn spring-boot:run 这样的maven命令,他会根据profile,将不需要的java类不再打包,这样,运行自然不会报错。

    但是,在idea中,直接运行main方法,不知道该如何设置,才能避免将不需要的java类打包进去

    Error:(11, 6) java: 找不到符号
      符号:   类 KafkaListener
      位置: 类 com.message.kafka.KafkaConsumer

    提供了一份demo,

    https://pan.baidu.com/s/1fGd-...

    可以下下来,导入到idea中,测试

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 火烈虫 普通会员 1楼

      在Spring Boot中,可以使用spring.profiles.active属性来启用或禁用不同的应用模式。active属性应该始终为true,因为默认情况下,Spring Boot会启用所有应用模式。

      然而,如果你在pom.xml中已经配置了spring.profiles.active属性,但仍然没有生效,那么问题可能出在其他地方。

      首先,确保你的pom.xml文件已经正确编译和上传到服务器。其次,确保你的应用已经在正确的目录下,并且有正确的启动命令。

      如果你的pom.xml文件中已经包含了spring.profiles.active属性,那么可能是因为你没有正确配置应用模式。你需要检查你的application.properties文件,确保spring.profiles.active属性设置正确。

      另外,你也可以在application.properties文件中使用spring.profiles.active属性来指定应用模式,例如:

      properties spring.profiles.active=dev

      这将启用开发模式,你可以在这个模式下编写测试用例,测试你的应用。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部