嵌入式tomcat使用

需要引入的maven依赖

 

<!--属性文件 -->
    <properties>
        <tomcat.version>8.5.24</tomcat.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>${tomcat.version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>Mytomcat</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- maven打包插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hope.redheart.wx.StartTomcat</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

 

 启动方法

public static void main(String[] args) throws ServletException, LifecycleException {

        int port = 80;

        if (args.length == 1) {
            port = Integer.parseInt(args[0]);
        }

        // 获取项目绝对路径
        File webappPath = new File(System.getProperty("user.dir"));

        // 创建tomat容器
        Tomcat tomcat = new Tomcat();

        // 设置端口
        tomcat.setPort(port);

        // 设置webapp目录
        StandardContext sct = (StandardContext) tomcat.addWebapp("/", webappPath.getAbsolutePath());

        // web项目路径
        WebResourceRoot resourceRoot = new StandardRoot(sct);

        // 相当于webapps中的一个项目文件夹
        DirResourceSet dirResourceSet = new DirResourceSet();

        // java字节码文件路径
        dirResourceSet.setBase(new File("target/classes").getAbsolutePath());
        // tomcat的默认写法 /WEB-INF/classes web项目的默认
        dirResourceSet.setWebAppMount("/WEB-INF/classes");

        // 这个项目文件夹的上一级目录,在tomcat中就是webapps
        dirResourceSet.setRoot(resourceRoot);

        dirResourceSet.setInternalPath("/");

        // 相当于把项目设置到webapp中
        resourceRoot.addPreResources(dirResourceSet);

        sct.setReloadable(false);

        // 一个web项目
        sct.setResources(resourceRoot);

        tomcat.start();
        System.out.println("Tomcat已在本地" + port + "端口启动!");

        // 方法阻塞,否则main方法运行完毕就退出,tomcat也就关闭了
        tomcat.getServer().await();
    }

 

posted @ 2019-10-25 16:54  RandomObject  阅读(963)  评论(0编辑  收藏  举报