(一) 创建 maven工程 的三种格式 基于spring boot框架

spring boot 是目前比较流行的java 后台开发框架,我从2017年开始接触。之前基于spring mvc 搭建了一套后台快速开发框架,有完整的权限用户管理登录,以及从网上找的 和自己写的一些 类库。后来将其改造为基于 spring boot 的开发框架。舍弃了用户 权限管理(用户 权限管理 使用单独的单点登录和权限管理集成),保留并丰富了之前的工具类库,如果你是一个开发新手。或者一个项目交付时间紧,可以考虑使用这个框架。他为很多工作中会遇到的技术问题,提供了解决方案,规避了很多坑。能够大幅提升开发效率。

一 创建工程

maven 版本 3.6
开发工具 idea 版本 2019.3
spirng boot 版本 2.1.11.RELEASE
spring cloud 版本 Greenwich.SR4
创建工程分三种
1、基于tomcat启动的 web工程
2、使用spring boot 内嵌tomcat 启动的web 工程(第三方包 静态资源 与代码分离)
3、使用 java -jar 命令启动的 非web工程。(第三方包 静态资源 与代码 分离)
三个工程创建完成如下:
在这里插入图片描述

(一)创建基于tomcat的web工程

首先点击 file > new > project 创建一个空工程
在这里插入图片描述
填写工程名和路径
在这里插入图片描述
然后在这页面点击 + 号 添加 module
在这里插入图片描述
传统的java web 项目(war包)选择 maven-archetype-webapps
在这里插入图片描述
添加工程名和路径 工程路径 是上面创建的空工程目录的子目录
在这里插入图片描述
创建完成之后是这个样子
在这里插入图片描述
点击ok

pom.xml 文件添加如下依赖

<dependency>
      <groupId>com.aheadframework</groupId>
      <artifactId>ahead-frame-jdbc</artifactId>
      <version>1.2.1-RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.aheadframework</groupId>
      <artifactId>ahead-frame-web</artifactId>
    <version>1.2.1-RELEASE</version>
 </dependency>

com.aheadframework 的这两个包是我自己打包的 已上传到maven中央仓库
https://mvnrepository.com/search?q=niupengyu
链接: https://mvnrepository.com/search?q=niupengyu.

源码位置 : https://github.com/niupengyu/ahead-framework.git.
在这里插入图片描述

pom 文件 build 为如下代码  此配置用于将 工程打包为war包
<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.1.11.RELEASE</version>
        <configuration>
          <fork>true</fork>
          <mainClass></mainClass>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
    <resources>
      <resource>
        <directory>src/main/webapp</directory>
        <targetPath>META-INF/resources</targetPath>
        <includes>
          <include>**/**</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/**</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>

启动类如下

package org.example.starter;


import com.aheadframework.core.listener.ApplicationFailedListener;
import com.aheadframework.core.listener.ApplicationPreparedListener;
import com.aheadframework.core.listener.ReadyEvent;
import com.aheadframework.core.listener.StartListener;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(exclude ={
        DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,
        MybatisAutoConfiguration.class,
        XADataSourceAutoConfiguration.class
})
@ComponentScan({"com.aheadframework","org.example"})
public class StarterWar extends SpringBootServletInitializer {
    public static void main(String[] args) {
        //LoggerContext loggerContext= (LoggerContext) LoggerFactory.getILoggerFactory();
        //loggerContext.getLogger("root").setLevel(Level.OFF);
        SpringApplication app = new SpringApplication(StarterWar.class);
        app.addListeners(new ApplicationFailedListener(), new ReadyEvent(),
                new ApplicationPreparedListener(),new StartListener());
        app.run(args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        builder.listeners(new ApplicationFailedListener(), new ReadyEvent(),
                new ApplicationPreparedListener(),new StartListener());
        return builder.sources(StarterWar.class);
    }
}

完整效果如下
在这里插入图片描述

(二)创建基于内嵌 tomcat的web工程

点击file > new >module
然后接着在主工程下 创建 第二个工程 是基于spring boot 内嵌tomcat 启动 依然是选择 maven-archetype-webapps 填写 路径在空工程路径之下
在这里插入图片描述
这个是maven 配置
在这里插入图片描述
pom 添加如下依赖

 <dependency>
      <groupId>com.aheadframework</groupId>
      <artifactId>ahead-frame-jdbc</artifactId>
      <version>1.2.1-RELEASE</version>
  </dependency>
  <dependency>
      <groupId>com.aheadframework</groupId>
      <artifactId>ahead-frame-web</artifactId>
    <version>1.2.1-RELEASE</version>
 </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.0.1.RELEASE</version>
  </dependency>

com.aheadframework 的这两个包是我自己打包的 已上传到maven中央仓库
https://mvnrepository.com/search?q=niupengyu
链接: https://mvnrepository.com/search?q=niupengyu.

源码位置 : https://github.com/niupengyu/ahead-framework.git.
pom packaging 参数改完jar

<packaging>jar</packaging>

pom 文件 build 为如下代码 此配置用于将 工程打包为jar包

<build>
        <plugins>
            <!--打包jar-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <!--不打包资源文件-->
                    <excludes>
                        <exclude>*.**</exclude>
                        <exclude>*/*.xml</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--jar包不包含唯一版本标识-->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!--指定入口类-->
                            <mainClass>这里是启动类位置</mainClass>
                        </manifest>
                        <manifestEntries>
                            <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                            <Class-Path>./resources/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}/${name}</outputDirectory>
                </configuration>
            </plugin>

            <!--拷贝依赖 copy-dependencies-->
            <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}/${name}/lib/
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--拷贝资源文件 copy-resources-->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/${name}/config</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-webapp</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/webapp</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/${name}/static</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.11.RELEASE</version>
                <configuration>
                    <!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
                    <includes>
                        <include>
                            <groupId>null</groupId>
                            <artifactId>null</artifactId>
                        </include>
                    </includes>
                    <layout>ZIP</layout>
                    <!--使用外部配置文件,jar包里没有资源文件-->
                    <addResources>true</addResources>
                    <outputDirectory>${project.build.directory}/${name}</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
                            <!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
                            <!--<classifier>run</classifier>-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <!-- 打包时将jsp文件拷贝到META-INF目录下-->
            <resource>
                <!-- 指定resources插件处理哪个目录下的资源文件 -->
                <directory>src/main/webapp</directory>
                <!--注意此次必须要放在此目录下才能被访问到-->
                <!--<targetPath>${project.build.directory}/${name}/static</targetPath>-->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <!--<resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>-->
        </resources>
    </build>

这种打包方式打包之后 结构如下
在这里插入图片描述
config 为配置文件所在目录
lib 为第三方jar所在目录
static 为静态资源 html 图片 css js 等所在目录

启动类如下

package org.example.starter;


import com.aheadframework.core.listener.ApplicationFailedListener;
import com.aheadframework.core.listener.ApplicationPreparedListener;
import com.aheadframework.core.listener.ReadyEvent;
import com.aheadframework.core.listener.StartListener;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(exclude ={
        DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,
        MybatisAutoConfiguration.class,
        XADataSourceAutoConfiguration.class
})
@ComponentScan({"com.aheadframework","org.example"})
public class StarterJar {
    public static void main(String[] args) {
        //LoggerContext loggerContext= (LoggerContext) LoggerFactory.getILoggerFactory();
        //loggerContext.getLogger("root").setLevel(Level.OFF);
        SpringApplication app = new SpringApplication(StarterJar.class);
        app.addListeners(new ApplicationFailedListener(), new ReadyEvent(),
                new ApplicationPreparedListener(),new StartListener());
        app.run(args);
    }

}

完整效果如下
在这里插入图片描述

(三)创建基于java -jar 命令启动的 非web工程

点击file > new >module
创建一个jar 包工程 (非web)
这里选择 maven-archetype-quickstart
在这里插入图片描述

添加工程名和路径 路径在空工程路径之下
在这里插入图片描述
maven配置如下
在这里插入图片描述

pom 添加如下依赖

 <dependency>
      <groupId>com.aheadframework</groupId>
      <artifactId>ahead-frame-jdbc</artifactId>
      <version>1.2.1-RELEASE</version>
 </dependency>

com.aheadframework 的这两个包是我自己打包的 已上传到maven中央仓库
https://mvnrepository.com/search?q=niupengyu
链接: https://mvnrepository.com/search?q=niupengyu.

源码位置 : https://github.com/niupengyu/ahead-framework.git.
pom packaging 参数改完jar

<packaging>jar</packaging>

pom 文件 build 为如下代码 此配置用于将 工程打包为jar包

<build>
        <plugins>
            <!--打包jar-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <!--不打包资源文件-->
                    <excludes>
                        <exclude>*.**</exclude>
                        <exclude>*/*.xml</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--jar包不包含唯一版本标识-->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!--指定入口类-->
                            <mainClass>这里是启动类位置</mainClass>
                        </manifest>
                        <manifestEntries>
                            <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                            <Class-Path>./resources/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}/${name}</outputDirectory>
                </configuration>
            </plugin>

            <!--拷贝依赖 copy-dependencies-->
            <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}/${name}/lib/
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--拷贝资源文件 copy-resources-->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/${name}/config</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-webapp</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/webapp</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/${name}/static</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.11.RELEASE</version>
                <configuration>
                    <!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
                    <includes>
                        <include>
                            <groupId>null</groupId>
                            <artifactId>null</artifactId>
                        </include>
                    </includes>
                    <layout>ZIP</layout>
                    <!--使用外部配置文件,jar包里没有资源文件-->
                    <addResources>true</addResources>
                    <outputDirectory>${project.build.directory}/${name}</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
                            <!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
                            <!--<classifier>run</classifier>-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <!-- 打包时将jsp文件拷贝到META-INF目录下-->
            <resource>
                <!-- 指定resources插件处理哪个目录下的资源文件 -->
                <directory>src/main/webapp</directory>
                <!--注意此次必须要放在此目录下才能被访问到-->
                <!--<targetPath>${project.build.directory}/${name}/static</targetPath>-->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <!--<resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>-->
        </resources>
    </build>

这种打包方式打包之后 结构如下
在这里插入图片描述
config 为配置文件所在目录
lib 为第三方jar所在目录
启动类如下

package org.example.starter;


import com.aheadframework.core.listener.ApplicationFailedListener;
import com.aheadframework.core.listener.ApplicationPreparedListener;
import com.aheadframework.core.listener.ReadyEvent;
import com.aheadframework.core.listener.StartListener;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(exclude ={
        DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,
        MybatisAutoConfiguration.class,
        XADataSourceAutoConfiguration.class
})
@ComponentScan({"com.aheadframework","org.example"})
public class StarterJar {
    public static void main(String[] args) {
        //LoggerContext loggerContext= (LoggerContext) LoggerFactory.getILoggerFactory();
        //loggerContext.getLogger("root").setLevel(Level.OFF);
        SpringApplication app = new SpringApplication(StarterJar.class);
        app.addListeners(new ApplicationFailedListener(), new ReadyEvent(),
                new ApplicationPreparedListener(),new StartListener());
        app.run(args);
    }

}

完整效果如下

在这里插入图片描述

以上是三种创建工程的方法。下一贴讲配置文件配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值