小言_互联网的博客

IDEA按照开发环境打包代码,docker版本

379人阅读  评论(0)

根据IDEA的开发环境打包代码,例如dev(本地),test(测试),pro(正式)

截图

微服务(配置文件托管给了git)

以dev为例子,文件内容为:

spring:
 cloud:
    config:
     name: mall #服务名
     profile: dev #profile名 可选值:dev test prod
     label: dev #标签   测试分支
     uri: #测试ip
     retry:
        initial-interval: 2000
        max-attempts: 5

配置文件截图:

<properties>
 <!-- 镜像的版本,根据小时区分 -->
 <maven.build.timestamp.format>yyyyMMddHH</maven.build.timestamp.format>
 </properties>
<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!--打包成可执行jar-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                    <fork>true</fork>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin ,不清楚可以参考官网配置 -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>

                <version>1.1.1</version>
                <configuration>
                	 <!-- 以腾讯云的镜像仓库为例子 -->
                    <imageName>ccr.ccs.tencentyun.com/项目名称-${filter-resource-name}/${project.build.finalName}:${maven.build.timestamp}</imageName>
                     <!-- 基础镜像 -->
                    <baseImage>ccr.ccs.tencentyun.com/项目名称-prod/java:8u201</baseImage>
                    <entryPoint>["java", "-jar","-XX:ErrorFile=/data/logs/jvm/微服务名称_hs_err_pid.log","/usr/local/项目名称/${project.build.finalName}/${project.build.finalName}.jar"]
                    </entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/usr/local/项目名称/${project.build.finalName}/</targetPath>
                            <directory>./target/</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
        <filters>
            <filter>src/main/resources/bootstrap-${filter-resource-name}.yml</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>filters/*</exclude>
                    <exclude>filters/*</exclude>
                    <exclude>bootstrap-dev.yml</exclude>
                    <exclude>bootstrap-test.yml</exclude>
                    <exclude>bootstrap-ready.yml</exclude>
                    <exclude>bootstrap-prod.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>bootstrap-${filter-resource-name}.yml</include>
                </includes>
            </resource>
        </resources>
    </build>

    <profiles>
        <!-- 本地跑 -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <filter-resource-name>dev</filter-resource-name>
            </properties>
        </profile>

        <!-- 测试 -->
        <profile>
            <id>test</id>
            <properties>
                <filter-resource-name>test</filter-resource-name>
            </properties>
        </profile>

        <!-- 预发布 -->
        <profile>
            <id>ready</id>
            <properties>
                <filter-resource-name>ready</filter-resource-name>
            </properties>
        </profile>

        <!-- 正式版 -->
        <profile>
            <id>prod</id>
            <properties>
                <filter-resource-name>prod</filter-resource-name>
            </properties>
        </profile>
    </profiles>

单点项目

配置文件截图:

<!-- 避免maven install的时候报找不到web.xml的异常 -->
    <build>
        <!--此节点解决myeclipse项目搬到IDEA下不能用的问题-->
        <finalName>项目名称</finalName>
        <resources>
            <resource>
                <directory>src</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>config</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>resources/${profiles.active}</directory>
            </resource>
        </resources>
        <sourceDirectory>src</sourceDirectory>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <webResources>
                        <resource>
                            <directory>WebRoot</directory>
                        </resource>
                        <resource>
                            <directory>resources/${package.environment}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <!-- java编译插件 -->


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <extdirs>WebRoot/WEB-INF/lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <!-- docker的maven插件,官网: https://github.com/spotify/docker-maven-plugin ,不清楚可以参考官网配置 -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>

                <version>1.1.1</version>
                <configuration>
                    <!--腾讯云仓库地址(自适应)-->
                    <imageName>ccr.ccs.tencentyun.com/项目名称-dev/项目名称:${maven.build.timestamp}</imageName>
                    <!-- 基础镜像 -->
                    <baseImage>ccr.ccs.tencentyun.com/项目名称-prod/tomcat:8.0.53</baseImage>
                    <resources>
                        <resource>
                            <targetPath>/usr/local/tomcat/webapps/</targetPath>
                            <directory>./target/</directory>
                            <include>项目名称.war</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- 开发环境 -->
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>prod</id>
            <properties>
                <package.environment>prod</package.environment>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境定时器 -->
            <id>timer</id>
            <properties>
                <package.environment>timer</package.environment>
            </properties>
        </profile>
    </profiles>

注意:以上demo只是集成了IDEA根据环境打镜像,如果要用docker插件打镜像并push到远程仓库,必须本地电脑安装docker环境,我的是win10,参考:win10安装docker


转载:https://blog.csdn.net/weixin_39388918/article/details/102486564
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场