maven项目中使用lib导入本地包在打包后不好使问题解决

Maven项目在idea中启动时,可以正常启动,但是在打包后发布到服务器时,就会报错。

原因是:本地的jar包并打包时并没有正常打包进去

需要在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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>connect</artifactId>
        <groupId>com.zitoo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.zitoo.connect</groupId>
    <artifactId>chongqin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>chongqin-server</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--        <dependency>-->
        <!--            <groupId>com.microsoft.sqlserver</groupId>-->
        <!--            <artifactId>mssql-jdbc</artifactId>-->
        <!--            <version>10.2.0.jre8</version>-->
        <!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>com.zitoo</groupId>
            <artifactId>zt-base</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
            <scope>system</scope>
            <systemPath>${pom.basedir}/lib/sqljdbc4-4.0.jar</systemPath>
        </dependency>
        <!--for mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
    </dependencies>

    <build>

        <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                    <includeSystemScope>true</includeSystemScope>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>


    </build>
</project>

如图:可以正常打包,并运行

### 回答1: 将Maven项目打包成第三方JAR包并将POM文件的依赖项打入其,可以按照以下步骤进行操作: 1. 在Maven项目的pom.xml文件,设置打包类型为jar,并确保所有依赖项正确配置。 2. 执行Maven打包命令,生成JAR文件。在项目根目录下运行以下命令: ``` mvn clean package ``` 3. Maven将根据pom.xml文件的配置,将JAR包和相关的依赖项一并打包到target目录的生成的JAR文件。 4. 在生成的JAR文件可以找到META-INF目录,其包含MANIFEST.MF文件。 5. 对MANIFEST.MF文件进行编辑,添加以下内容: ``` Class-Path: <path-to-dependency-jar1> <path-to-dependency-jar2> ... ``` 其,`<path-to-dependency-jar>`是各个依赖项JAR文件的相对或绝对路径。请确保路径正确并在每个JAR文件之间使用空格进行分隔。 6. 保存并关闭MANIFEST.MF文件。 7. 最后,可以将生成的JAR文件作为第三方JAR包进行使用,并且该JAR包已经包含了所有的依赖项JAR文件。 总结:通过适当的配置和编辑MANIFEST.MF文件,可以将Maven项目打包成第三方JAR包,并将pom.xml的依赖项打包入其,从而方便在其他项目引用该JAR包。 ### 回答2: Maven是一个Java项目管理工具,可以用来管理项目的依赖、构建和打包。将项目打包为第三方jar包并将pom依赖包打入进来的方法如下: 首先,在项目的pom.xml文件配置打包属性。将项目打包方式设置为jar,在该项目的构建插件添加依赖插件。例如: ```xml <build> <plugins> <!-- 设置打包方式为jar --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 添加依赖插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 其次,运行以下Maven命令进行打包: ```bash mvn clean package ``` 成功运行后,会在项目的target目录下生成一个打包好的jar文件。在该目录下还会有一个lib目录,其包含了所有项目依赖的jar文件。 最后,可以将生成的jar包和lib目录一起打包或发布,使其作为第三方jar包使用。其他使用者只需要导入该jar文件,并将依赖的jar文件一同放入classpath,即可使用项目的功能。 通过以上步骤,我们可以将一个Java项目打包为第三方jar包,并将pom依赖包一同打入进来,方便其他项目使用和引入依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值