springboot项目中使用maven resources

maven resource 组件可以把pom的变量替换到相关的resouces目录中的资源文件变量

示例项目:内容中心 (文章管理)  生成jar包,生成docker ,生成k8s文件

1.项目结构

├── content-api
│   ├── pom.xml
│   └── src
│       ├── main
│       │   └── java
│       │       └── com
│       │           └── itstudy
│       │               └── content_api
│       │                   ├── domain
│       │                   │   └── ArticleInfo.java
│       │                   └── service
│       │                       └── ArticleService.java
│       └── test
│           └── java
│               └── com
│                   └── itstudy
│                       └── content_api
│                           └── AppTest.java
├── content-server
│   ├── pom.xml
│   └── src
│       ├── bin
│       │   └── app.sh
│       ├── main
│       │   ├── java
│       │   │   └── com
│       │   │       └── itstudy
│       │   │           └── content_server
│       │   │               ├── ContentServerApp.java
│       │   │               ├── controller
│       │   │               │   └── ArticleController.java
│       │   │               └── service
│       │   │                   └── impl
│       │   │                       └── ArticleServiceImpl.java
│       │   └── resources
│       │       ├── dev
│       │       │   ├── application.yml
│       │       │   ├── docker
│       │       │   │   └── Dockerfile
│       │       │   └── k8s
│       │       │       ├── content-center-rc.yml
│       │       │       └── content-center-svc.yml
│       │       └── pro
│       │           ├── application.yml
│       │           ├── docker
│       │           │   └── Dockerfile
│       │           └── k8s
│       │               ├── content-center-rc.yml
│       │               └── content-center-svc.yml
│       └── test
│           └── java
│               └── com
│                   └── itstudy
│                       └── content_api
│                           └── ContentServerAppTest.java
└── pom.xml

2. 父级项目pom文件 

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itstudy</groupId>
    <artifactId>contentcenter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>content-api</module>
        <module>content-server</module>
    </modules>

    <packaging>pom</packaging>
    <name>contentcenter</name>

    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Camden.SR5</version>
        <relativePath></relativePath>
    </parent>

    <distributionManagement>
        <!-- 两个ID必须与 setting.xml中的<server><id>nexus-releases</id></server>保持一致 -->
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://maven.mysite.com/nexus/content/repositories/demo-Release/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://maven.mysite.com/nexus/content/repositories/demo-Snapshot/</url>
        </snapshotRepository>
    </distributionManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 使用dockerfile方式
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.11</version>
                <configuration>
                    <imageName>dockerimage.mysite.com/contentcenter/${project.artifactId}</imageName>
                    <imageTags>contentcenter-${project.version}</imageTags>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <serverId>docker-image</serverId>
                    <useConfigFile>true</useConfigFile>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            -->
            <!--使用配置方式-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.11</version>
                <configuration>
                    <imageName>dockerimage.mysite.com/contentcenter/${project.artifactId}</imageName>
                    <baseImage>dockerimage.mysite.com/java/alpine-oraclejdk8:8.131.11-slim</baseImage>
                    <imageTags>contentcenter-${project.version}</imageTags>
                    <maintainer>my@mail.com</maintainer>
                    <entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom","-jar", "${project.build.finalName}.jar"]</entryPoint>
                    <volumes>
                        <volume>/tmp</volume>
                    </volumes>
                    <serverId>docker-image</serverId>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.2实体类

package com.itstudy.content_api.domain;

public class ArticleInfo {
    private String id;
    private String content;

    //省略geter setter

}

2.3接口

package com.itstudy.content_api.service;

import com.itstudy.content_api.domain.ArticleInfo;

public interface ArticleService {

    void insert(ArticleInfo item);

    void update(ArticleInfo item);

}

3 content-server项目 具体业务服务

3.1 pom文件 

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>contentcenter</artifactId>
        <groupId>com.itstudy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>content-server</artifactId>

    <name>content-server</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.itstudy</groupId>
            <artifactId>content-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <profile-name>pro</profile-name>
            <app-mongodb-replica-set>192.168.146.8:27017,192.168.146.9:27017,192.168.146.10:27017</app-mongodb-replica-set>
            <app-mongodb-credentials>username:password@dbname-test</app-mongodb-credentials>
            <app-mongob-db>dbname-test</app-mongob-db>
            <plateform-ds>http://192.168.146.76:50002/eureka/</plateform-ds>
            <app-dfs1>192.168.146.11:22122</app-dfs1>
            <app-dfs2>192.168.146.11:22122</app-dfs2>
            <app-dfs-sotimeout>1501</app-dfs-sotimeout>
            <app-dfs-connecttimeout>601</app-dfs-connecttimeout>
            <app-name>${project.name}</app-name>
            <app-port>8080</app-port>
        </properties>
    </profile>
    <profile>
        <id>pro</id>
        <properties>
            <profile-name>pro</profile-name>
            <app-mongodb-replica-set>192.168.146.8:27017,192.168.146.9:27017,192.168.146.10:27017</app-mongodb-replica-set>
            <app-mongodb-credentials>username:password@dbname</app-mongodb-credentials>
            <app-mongob-db>dbname</app-mongob-db>
            <plateform-ds>http://ds:8761/eureka/</plateform-ds>
            <app-dfs1>192.168.146.11:22122</app-dfs1>
            <app-dfs2>192.168.146.11:22122</app-dfs2>
            <app-dfs-sotimeout>1501</app-dfs-sotimeout>
            <app-dfs-connecttimeout>601</app-dfs-connecttimeout>
            <app-port>8080</app-port>
            <app-name>${project.name}</app-name>
        </properties>
    </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>dev/**</exclude>
                    <exclude>pro/**</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/resources/${profile-name}</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                    <include>**/Dockerfile</include>
                </includes>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/resources/</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!--不加此组件,会导至jar包启动时找不到manifest-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build> 

</project>

3.2  service.impl

package com.itstudy.content_server.service.impl;

import com.itstudy.content_api.domain.ArticleInfo;
import com.itstudy.content_api.service.ArticleService;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Override
    public void insert(ArticleInfo item) {

    }

    @Override
    public void update(ArticleInfo item) {

    }
}

3.3 controller

package com.itstudy.content_server.controller;

import com.itstudy.content_api.domain.ArticleInfo;
import com.itstudy.content_api.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @PostMapping("/insert")
    public void postInsert(@RequestBody ArticleInfo info){
        //其它业务
    }

    @PostMapping("/update")
    public void postUpdate(@RequestBody ArticleInfo info){
        //其它业务
    }
}

3.4 springboot 启动类app

package com.itstudy.content_server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * 项目启动类
 */
@SpringBootApplication
@EnableEurekaClient
public class ContentServerApp
{
    public static void main( String[] args )
    {
        System.out.println("内容中心-启动中...");

        SpringApplication.run(ContentServerApp.class, args);

        System.out.println("内容中心-启动成功");
    }
}

3.5 resources目录

3.5.1 resources/dev/docker/Dockerfile

FROM dockerimage.mysite.com/java/alpine-oraclejdk8:8.131.11-slim
VOLUME /tmp
ADD  @project.build.finalName@.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"

3.5.2 resource/dev/k8s/rc 及 resources/dev/k8s/svc

1) rc文件

apiVersion: v1

kind: ReplicationController

metadata:

  name: @project.artifactId@

spec:

  replicas: 1

  selector:

    name: @project.artifactId@

  template:

    metadata:

      labels:

        name: @project.artifactId@

    spec:
      containers:

        - name: api

          image: dockerimage.mysite.com/@app-name@/@app-name@
          ports:
            - containerPort: 8080

2)svc文件

apiVersion: v1

kind: Service

metadata:

  name: @app-name@  #也可替换为 @project.artifactId@

spec:

  selector:

    name: @app-name@ #也可替换为 @project.artifactId@

  ports:

    - name: http

      port: 8080

      protocol: TCP
      nodePort: 30008
  type: NodePort

3.6 application.yml文件 

server:
  port: @app-port@
spring:
  application:
    name:  @app-name@  # 项目名称尽量用小写
  jmx:
    enabled: false
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: false
eureka:
  client:
    serviceUrl:
      defaultZone: @plateform-ds@    # 指定注册中心的地址
  instance:
    preferIpAddress: true
fdfs:
  soTimeout: @app-dfs-sotimeout@
  connectTimeout: @app-dfs-connecttimeout@
  thumbImage:             #缩略图生成参数
    width: 150
    height: 150
  trackerList:            #TrackerList参数,支持多个
    - @app-dfs1@
    - @app-dfs2@

3.7 resource/pro文件夹与dev文件夹文件相似,只是个别配置不同。

 

4.重要总结

4.1 在pom文件中,引用maven变量使用 ${变量名}即可

4.2 在springboot项目中,使用maven resources组件时,使用@变量名@ 进行取值(因为springboot项目中进行了特殊处理)

4.3 在使用maven resources组件时

<excludes>
<exclude>dev/**</exclude>
<exclude>pro/**</exclude>
</excludes>

* 匹配 0或N个字符
** 匹配 0或N个目录
如果使用*不能实现相应功能,请切换** 即可

4.4 在使用maven resources组件时,只要增加了resource这个节点,必须重新指定resources文件夹位置 

如示例: https://www.cnblogs.com/liuxm2017/p/10688789.html

 

posted @ 2019-06-24 17:48  liuxm-刘小明  阅读(3788)  评论(0编辑  收藏  举报