4
日期作者版本备注
2020-09-27dingbinthu@163.comV1.0

maven项目引入本地jar包问题从3个层面考虑:

1. IDE工具如Jetbrains IDEA考虑:

依次打开IDEA的File – Project Structure – Project Settings – Modules –
Dependencies 选项卡区域,点击右上角+号图标,选择1.JARS or
directories…,再选择java工程根目录下的lib或libs目录下,把所有需要本地引入的jar包都多选中,
然后点击OK即可。

image
image

2. Maven编译层面考虑:

在pom.xml文件中dependencies组件的最后部分添加如下图的本地依赖jar包。

注意scope为system表示本地的jar包。
systemPath为本地路径,一定要用${project.basedir}/开头引入,以表通用型。
image
image

以上3个地方如此修改后,联合的效果是,mvn clean package
后会target目录下生成一个lib目录,并且把所有的远程jar包和本地jar包都拷贝到lib目录下,以供后期java程序运行时找到之用。注意本地jar包会以上图中绿色框的约定的命名方式出现在lib目录下。

image

上图中<addClasspath>true</addClasspath>表明会在生成的目标jar文件中MANIFEST.MF中添加classpath如下图,<classpathPrefix>lib</classpathPrefix>
表明会给MANIFEST.MF中的classpath中每一个classpath加一个前缀lib.这些特性都非常重要。

因此<plugins></plugins>组件下一共有3项:
image

image

以vim 打开生成的目标jar包vi mylucenedemo.jar:后:
image

光标放在MANIFEST.MF上按下回车,即可看到:
image

此外,如果有需要的话,java工程目标jar的运行可能还依赖于一些配置文件,通过pom.xml中profiles下的profile将其拷贝到target目录下的conf目录中去:
image
效果如下图:
image

3. 目标jar程序执行层面考虑:

接下来,就可以在java工程的根目录下执行:mvn clean install -U, 会生成如下的target目录,有用的是conf目录,lib目录和目标文件mylucenedemo.jar.

image

执行方式是: 在target目录下直接执行java -jar mylucenedemo.jar即可。

image

会自动读取当前目下的lib子目录的所有依赖jar包,包括引入的本地jar包。以及conf目录下的所有配置文件。

最后,附上本文中示例的pom.xml文件全文内容如下:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.free</groupId>
    <artifactId>lucenedemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mysql.driver.version>5.1.30</mysql.driver.version>
    </properties>

    <dependencies>

<!--        <dependency>-->
<!--            <groupId>org.apache.lucene</groupId>-->
<!--            <artifactId>lucene-core</artifactId>-->
<!--            <version>8.6.2</version>-->
<!--        </dependency>-->

<!--        <dependency>-->
<!--            <groupId>org.apache.lucene</groupId>-->
<!--            <artifactId>lucene-demo</artifactId>-->
<!--            <version>8.6.2</version>-->
<!--        </dependency>-->
        <!--一般分词器,适用于英文分词-->
<!--        <dependency>-->
<!--            <groupId>org.apache.lucene</groupId>-->
<!--            <artifactId>lucene-analyzers-common</artifactId>-->
<!--            <version>8.6.2</version>-->
<!--        </dependency>-->
        <!--中文分词器-->
<!--        <dependency>-->
<!--            <groupId>org.apache.lucene</groupId>-->
<!--            <artifactId>lucene-analyzers-smartcn</artifactId>-->
<!--            <version>8.6.2</version>-->
<!--        </dependency>-->

        <!--对分词索引查询解析-->
<!--        <dependency>-->
<!--            <groupId>org.apache.lucene</groupId>-->
<!--            <artifactId>lucene-queryparser</artifactId>-->
<!--            <version>8.6.2</version>-->
<!--        </dependency>-->
        <!--检索关键字高亮显示-->
<!--        <dependency>-->
<!--            <groupId>org.apache.lucene</groupId>-->
<!--            <artifactId>lucene-highlighter</artifactId>-->
<!--            <version>8.6.2</version>-->
<!--        </dependency>-->


        <!-- 添加grpc相关依赖包 -->
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>1.24.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.24.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.24.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>

        <!-- jdbc driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.driver.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.turbine</groupId>
            <artifactId>turbine</artifactId>
            <version>4.0-M2</version>
        </dependency>

        <dependency>
            <groupId>avalon</groupId>
            <artifactId>avalon-framework</artifactId>
            <version>4.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.fulcrum</groupId>
            <artifactId>fulcrum-testcontainer</artifactId>
            <version>1.0.7</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
        </dependency>

        <dependency >
            <groupId >javax.mail </groupId >
            <artifactId >mail </artifactId >
            <version >1.4.5 </version >
        </dependency >
        <dependency >
            <groupId >com.sun.mail </groupId >
            <artifactId >javax.mail </artifactId >
            <version >1.5.4 </version >
        </dependency >
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-exec</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.javacsv</groupId>
            <artifactId>javacsv</artifactId>
            <version>2.0</version>
        </dependency>


        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>my-lucene-core</artifactId>
            <version>8.6.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/lucene-core-8.6.2-SNAPSHOT.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>my-lucene-demo</artifactId>
            <version>8.6.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/lucene-demo-8.6.2-SNAPSHOT.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>my-lucene-queryparser</artifactId>
            <version>8.6.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/lucene-queryparser-8.6.2-SNAPSHOT.jar</systemPath>
        </dependency>

    </dependencies>




    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- <version>0.0.1</version> -->
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <extdirs>${project.basedir}/lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>mylucenedemo</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <finalName>mylucenedemo</finalName>
                            <archive>
                                <manifest>
                                    <addClasspath>true</addClasspath>
                                    <classpathPrefix>lib</classpathPrefix>
                                    <mainClass>com.freedom.lucene.demo.LuceneLevenshteinDemo</mainClass>
                                    <useUniqueVersions>false</useUniqueVersions>
                                </manifest>

                                <!--add local jar package to MANIFEST.MF-->
                                <manifestEntries>
                                    <Class-Path> lib/my-lucene-demo-8.6.2.jar lib/my-lucene-queryparser-8.6.2.jar lib/my-lucene-core-8.6.2.jar </Class-Path>
                                </manifestEntries>

                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <profile>
            <id>config-package</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.7</version>
                        <executions>
                            <execution>
                                <id>config-package</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <copy todir="${project.build.directory}/conf">
                                            <fileset file="${project.basedir}/src/main/resources/c3p0-config.xml" />
                                        </copy>
                                        <copy todir="${project.build.directory}/conf">
                                            <fileset file="${project.basedir}/src/main/resources/log4j.properties" />
                                        </copy>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>

</project>

apollo008
151 声望9 粉丝

A programmer who loves life