美文网首页
SpringBoot同时打jar包和war包

SpringBoot同时打jar包和war包

作者: _Gaara_ | 来源:发表于2020-09-22 10:52 被阅读0次

根据公司业务需求,需要根据不同环境打出不同的包,比如本地要jar的原生启动方式,测试环境则需要weblogic部署的war包,故此有此修改
添加依赖:

        <!--添加servlet-api的依赖,用来打war包  -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

pom修改:

<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>war</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!-- profile对资源的操作 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>application*.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 是否替换@xx@表示的maven properties属性值 -->
                <filtering>true</filtering>
                <includes>
                    <include>application.yml</include>
                    <include>application-${spring.profiles.active}.yml</include>
                </includes>
            </resource>
        </resources>
    </build>

    <!--多环境配置文件-->
    <profiles>
        <!-- 开发环境 -->
        <profile>
            <id>local</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <version>RELEASE</version>
                    <exclusions>
                        <exclusion>
                            <groupId>org.apache.logging.log4j</groupId>
                            <artifactId>log4j-to-slf4j</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
            <properties>
                <spring.profiles.active>local</spring.profiles.active>
            </properties>
            <activation>
                <!-- 默认使用local -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 集成测试环境 -->
        <profile>
            <id>test</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <version>RELEASE</version>
                    <exclusions>
                        <exclusion>
                            <groupId>org.apache.logging.log4j</groupId>
                            <artifactId>log4j-to-slf4j</artifactId>
                        </exclusion>
                        <exclusion>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-tomcat</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
        <!-- 发布测试环境 -->
        <profile>
            <id>release</id>
            <properties>
                <spring.profiles.active>release</spring.profiles.active>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prd</id>
            <properties>
                <spring.profiles.active>prd</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

这部分在test环境需要剔除Tomcat,并且配置一个生成war包的插件
在启动类里做如下修改:


@EnableTransactionManagement
@SpringBootApplication 
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}

相关文章

网友评论

      本文标题:SpringBoot同时打jar包和war包

      本文链接:https://www.haomeiwen.com/subject/qqtwyktx.html