Springboot创建子父工程过程图解

Springboot创建子父工程过程图解

综合资料网友投稿2020-02-19 11:00:18282

这篇文章主要介绍了springboot创建子父工程过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.创建子父工程

2.添加pom配置文件

2.1 父工程pom.xml

<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.demo</groupId>
 <artifactId>springboot</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>pom</packaging>
 <modules>
   <module>springboot-server</module>
   <module>springboot-common</module>
   <module>springboot-domain</module>
   <module>springboot-sdk</module>
 </modules>
 
 
 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <build.jdk.version>1.8</build.jdk.version>
    <spring.boot.version>1.5.9.RELEASE</spring.boot.version>
    <spring.version>4.3.13.RELEASE</spring.version>
    <slf4j.version>1.7.25</slf4j.version>
    <fastjson.version>1.2.8</fastjson.version>
    <commons.lang.version>3.1</commons.lang.version>
    <commons.io.version>2.5</commons.io.version>
    <commons.beanutils.version>1.9.3</commons.beanutils.version>
    <commons.codec.version>1.11</commons.codec.version>
    <guava.version>19.0</guava.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>${spring.boot.version}</version>
      </dependency>

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>${spring.boot.version}</version>
      </dependency>

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
      </dependency>

      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
      </dependency>

      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
      </dependency>


      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons.lang.version}</version>
      </dependency>

      <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${commons.io.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>${commons.beanutils.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${commons.codec.version}</version>
      </dependency>

      <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <version>${spring.boot.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
  </dependencies>

  <distributionManagement>
    <repository>
      <id>nexus-releases</id>
      <name>Nexus Release Repository</name>
      <url>http://ip:9090/repository/releases/</url>
    </repository>
    <snapshotRepository>
      <id>nexus-snapshots</id>
      <name>Nexus Snapshot Repository</name>
      <url>http://ip:9090/repository/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${build.jdk.version}</source>
          <target>${build.jdk.version}</target>
        </configuration>
      </plugin>

      <!-- 使用方式命令: versions:set -DoldVersion=* -DnewVersion=1.0.1-SNAPSHOT -DprocessAllModules=true 
        -DallowSnapshots=true -->
      <plugin>
        <groupId>org.sonarsource.scanner.maven</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>3.2</version>
      </plugin>
    </plugins>
  </build>
  
</project>

2.2 子工程Springboot-server pom.xml:

<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>
 <parent>
  <groupId>com.demo</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>
 <artifactId>springboot-server</artifactId>
 
 <name>springboot-server</name>
 <properties>
    <application.main.class>com.demo.Application</application.main.class>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
    </dependency>
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
    </dependency>

    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.10.5</version>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>*.properties</include>
          <include>*.yml</include>
          <include>*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <excludes>
          <exclude>scripts/*</exclude>
          <exclude>*.properties</exclude>
          <exclude>*.yml</exclude>
          <exclude>*.xml</exclude>
        </excludes>
      </resource>

      <!-- 收集运行脚本 -->
      <resource>
        <directory>src/main/resources/scripts</directory>
        <targetPath>${project.build.directory}/bin</targetPath>
        <filtering>true</filtering>
        <includes>
          <include>*.sh</include>
          <include>*.bat</include>
        </includes>
      </resource>
      <!-- 收集配置文件 -->
      <resource>
        <directory>src/main/resources</directory>
        <targetPath>${project.build.directory}/conf</targetPath>
        <filtering>true</filtering>
        <includes>
          <include>*.properties</include>
          <include>*.yml</include>
          <include>*.xml</include>
        </includes>
      </resource>

      <!-- 收集手动导入的jar包 -->
      <resource>
        <directory>lib</directory>
        <targetPath>${project.build.directory}/lib</targetPath>
      </resource>
    </resources>
    <plugins>
      <!-- 1.用于编译的plugin -->
      <!-- <plugin> -->
      <!-- <groupId>org.apache.maven.plugins</groupId> -->
      <!-- <artifactId>maven-compiler-plugin</artifactId> -->
      <!-- <version>3.1</version> -->
      <!-- <configuration> -->
      <!-- <fork>true</fork> -->
      <!-- <source>1.8</source> -->
      <!-- <target>1.8</target> -->
      <!-- <encoding>UTF-8</encoding> -->
      <!-- </configuration> -->
      <!-- </plugin> -->

      <!-- 2.用于生成jar包的plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <outputDirectory>${project.build.directory}/lib</outputDirectory>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
            </manifest>
          </archive>
          <excludes>
            <exclude>*.xml</exclude>
            <exclude>*.yml</exclude>
            <exclude>*.properties</exclude>
          </excludes>
        </configuration>
      </plugin>

      <!-- 3.用于拷贝maven依赖(lib)的plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- 4.用于拷贝resource文件夹 的plugin -->

      <!-- 5.配置生成源代码jar的plugin[这里的源代码是指java文件,不是class文件] -->
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <attach>true</attach>
          <!-- 配置源代码jar文件的存放路径 -->
          <outputDirectory>${project.build.directory}</outputDirectory>
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- 打包zip -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>dfrp-portal</id>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>${application.main.class}</mainClass>
                </manifest>
              </archive>
              <descriptors>
                <descriptor>src/main/resources/scripts/assembly.xml</descriptor>
              </descriptors>
              <!-- 将依赖jar包都包含在目标jar中 -->
              <!-- <descriptorRefs> -->
              <!-- <descriptorRef>jar-with-dependencies</descriptorRef> -->
              <!-- </descriptorRefs> -->
              <finalName>${project.name}-${version}</finalName>
            </configuration>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
 
 
</project>

3.App启动类

/**
 * Copyright (c) 2020, All Rights Reserved.
 *
*/

package com.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
  private static final Logger LOG = LoggerFactory.getLogger(Application.class);

  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application.class);
    app.setBannerMode(Banner.Mode.OFF);
    app.setWebEnvironment(true);
    app.run(args);
    LOG.info("**************** Startup Success ****************");
  }
}

4.application.yml配置文件

version: ${project.version}

server:
 port: 7070
 session-timeout: 0
 context-path: /

5.测试controller类

package com.demo.server.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
  
  @RequestMapping("/helloworld")
  public String hello(String name) {
    return "name"+name;
  }

}

springboot子父项目启动成功。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持聚米学院。

本文转载自互联网,如侵犯您的权益请及时联系我们处理!
点击这里复制本文地址 版权说明

以上内容所选用的图片及资料均来源于网络或者用户投稿,聚米学院整理发布,本站不拥有此类资源的版权,如果侵犯了你的权益,请【 提交工单】或者联系邮箱([email protected]) 我们立即下架或删除。

支持Ctrl+Enter提交

聚米学院 © All Rights Reserved.