java项目打成jar包、war包、部署到服务器

mvc项目 + Tomcat 部署到服务器的总体过程:

1.本地使用maven打成war包;
2.在SecureCRT中打开SFTP窗口:
lpwd => 查看当前处于本地哪个目录 ,pwd => 查看线上服务器处于哪个目录;
首先进入线上服务器的目标目录,通过put xxx.war 就能上传到目标目录中;
3.通过 jar -xvf xxx.war将war包解压;

遇到的问题

1.NoSuchFieldError: Factory java.lang.NoSuchFieldError: Factory at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:197)
在tomcat的bin目录下执行:./nohup_start.sh
出现如下错误:
在这里插入图片描述
1)由于在tomcat中部署了好几个应用,所以当时怀疑是依赖包冲突的问题,于是把其他项目的lib包删除了(别学我,被批评了),但是我的应用还是报上图的错误;
2)点击ctrl+N ,搜索发现lite包和schemas包有相同名字的类 ,删除schemas依赖后成功启动。
在这里插入图片描述
此部分解决思路参考了:https://blog.csdn.net/yuanlairuci1992/article/details/125406863

添加映射

结构:ip地址 域名。
如果要访问域名,那就会直接访问对应的ip地址。

1.通过test.com访问测试环境,原本的ip地址是120.70.111.77,访问的端口是80,本地无需配置
2.通过test.com访问本地环境,用于调试,配置:127.0.0.1 test.com
命令行执行:(1)ipconfig /flushdns;(2)ping test.com 能收到127.0.0.1的回应就是配置成功了。
本地环境的端口:8087 , 测试环境的端口80,如果是这样的话是无法通过test.com访问本地的。必须要保持端口一致,把本地的端口修改为80就可以了。
C:\Windows\System32\drivers\etc\hosts文件

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
120.92.71.176       fdtest.dayainfo.com

项目打成jar包,供其他项目引入

pom.xml中的配置

<plugins>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <compilerArguments>
            <!-- 打包本地jar包 -->
            <extdirs>${basedir}/src/main/webapp/WEB-INF/lib</extdirs>
          </compilerArguments>
        </configuration>
      </plugin>
      <!-- 生成jar包,发现里面都是编译后的class文件。这种文件是看不到源码的(当然,你可以选择反编译)-->
      <!-- 默认jar包中不包含第三方依赖-->
      <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
<!-- 加入生成的源码jar包-->
<plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.2</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
</plugins>

maven运行:clean -> package -> install 加入到本地maven库

jar包和war包

工具:idea,当创建项目的时候,选择maven-archetype:
选择maven-webapp: 是mvc项目,包含前端web,默认打war包;适用情况:打成war包部署到tomcat上,是一个完整的项目;
选择maven-quick-start: 项目是纯后端代码,没有前端,默认打jar包;适用情况:项目最后打成jar包供其他项目调用;
使用maven-quick-start搭建的项目:把搭建完成后的org.example包全部删除,包含其下的主类,并添加自己的代码
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.xx.yy</groupId>
  <artifactId>项目名称,直接写英文</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <name>项目名称</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <poi.version>5.2.3</poi.version>
  </properties>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1.2</version>
        <configuration>
          <attach>true</attach>
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!--file read begin -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>${poi.version}</version>
    </dependency>
    <dependency>
      <groupId>com.dayainfo.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>5.2.3</version>
    </dependency>
  </dependencies>
</project>

生成的项目结构,打包结果如下:
在这里插入图片描述

需要在target目录中新增显示文件夹

**目的:**需要在src/main下新增文件夹,并且在后续前端需要访问;
正常情况下target目录中不会有对应的新增文件夹;
解决方法:
修改pom.xml文件:

<build>
        <resources>
            <resource>
                <directory>src/main/img</directory>
                <filtering>true</filtering>
                <targetPath>../images2</targetPath>
                <includes>
                    <include>**/*.png</include>
                </includes>
            </resource>
        </resources>
</build>

**结果如下:**能够在target目录中也显示
在这里插入图片描述

查看jar包文件内容

$ jar tf TicTacToe.jar

原模块结构:
在这里插入图片描述
打成jar包之后执行:
C:\Users\DELL>jar tf D:/xdocreport-xdocreport-parent-2.0.5/thirdparties-extension/fr.opensagres.poi.xwpf.converter.core/target/fr.opensagres.poi.xwpf.converter.core-2.0.5.jar
结果: 与classes的文件结构很相似
META-INF/
META-INF/MANIFEST.MF
xxx/
xxx/yyy/
xxx/yyy/poi/
xxx/yyy/poi/xwpf/
xxx/yyy/poi/xwpf/converter/core/XWPFDocumentVisitor$1.class
xxx/yyy/poi/xwpf/converter/core/XWPFDocumentVisitor.class

omml2mml.xsl
META-INF/maven/
META-INF/maven/fr.opensagres.xdocreport/
META-INF/maven/fr.opensagres.xdocreport/fr.opensagres.poi.xwpf.converter.core/
META-INF/maven/fr.opensagres.xdocreport/fr.opensagres.poi.xwpf.converter.core/pom.xml
META-INF/maven/fr.opensagres.xdocreport/fr.opensagres.poi.xwpf.converter.core/pom.properties

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值