1

我将 Maven 用于我的开发目的。我有一个要求,我想在我的项目 jar 中包含一些第三方 jar,并排除 pom.xml 文件中指定的其他 jar。下面是我的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.ckdm</groupId>
<artifactId>Exporter</artifactId>
<version>atlas2.1</version>
<packaging>jar</packaging>

<name>Exporter</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <aspectj.version>1.6.10</aspectj.version>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>        
</properties>

<build>
     <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            <executions>
                    <execution>
                            <id>make-assembly</id> 
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.ckdm</groupId>
        <artifactId>CubeCreator</artifactId>
        <version>atlas2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.guavus</groupId>
        <artifactId>ConcurrentFlows</artifactId>
        <version>atlas2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>ps</groupId>
        <artifactId>thriftGenerated</artifactId>
        <version>atlas2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>apache</groupId>
        <artifactId>libthrift</artifactId>
        <version>0.5.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
        <version>0.20.203.0</version>
    <scope>provided</scope> 
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>1.7.1</version>
  <scope>provided</scope>   
    </dependency>
<dependency>
        <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
    </dependency>
    <dependency>
            <groupId>ch.qos.logback</groupId>
        <artifactId>logback-access</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        <version>1.0.1</version>
</dependency>

</dependencies>

</project>

我在某处读到,指定的范围不包括项目 jar 中的 jar。我在哪里弄错了,它不能包含任何罐子?

4

4 回答 4

0

根据您的上述评论,我假设您有一个罐子,里面有您的主要课程。

在您的项目中添加MANIFEST.MF文件。其内容应如下所示:

Manifest-Version: 1.0
Class-path: YourMainJar.jar thirdparty1.jar thirdparty2.jar
Main-Class: com.test.mymainclass

YourMainJar.jar是您拥有主类的文件,第三方 jar 需要YourMainJar.jar位于放置您的相同位置。

当你双击你的YourMainJar.jar,它会自动运行并从同一位置选择你的第三方 jars。

MANIFEST.MF注意:当您将项目打包为 jar 时,您需要包含此内容。

于 2013-01-18T08:25:06.150 回答
0

Maven 文档provide

这很像 compile,但表明您希望 JDK 或容器在运行时提供依赖项。例如,在为 Java 企业版构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖设置为提供的范围,因为 Web 容器提供了这些类。此范围仅在编译和测试类路径上可用,并且不可传递。

通过使用 scope provided,它实际上假设容器将在运行时提供依赖。但并非您添加的所有依赖项实际上都是由容器提供的。

于 2013-01-18T07:03:55.600 回答
0

删除范围标记。

如果仍然有问题,请使用我的工作 xml。

  <!--               Build complete JAR with all dependencies         -->
        <!-- mvn clean install assembly:single -->
                <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>2.3</version>
                        <executions>
                            <execution>
                                <id>jar-with-dependencies</id>
                                <phase>install</phase>
                                <goals><goal>single</goal></goals>
                                <configuration>
                                    <archive>
                                        <manifest>
                                            <mainClass>com.mawia.YourMainClass</mainClass>
                                        </manifest>
                                    </archive>
                                    <descriptorRefs>
                                        <descriptorRef>jar-with-dependencies</descriptorRef>
                                    </descriptorRefs>
                                    <finalName>consumer</finalName>
                                    <appendAssemblyId>false</appendAssemblyId>
                                </configuration>
                            </execution>
                        </executions>
                </plugin>
于 2013-01-18T07:11:30.103 回答
0

“项目 jar”中的第三方 jar 是什么意思?如果您有任何要包含的第三方 jar,则通过添加来自 maven repo 或本地的依赖项来添加它们。

如果您不希望项目类路径中有任何 jar 文件,请删除其依赖项。

对于本地您使用:

<dependency>
        <groupId>org.test</groupId>
        <artifactId>testjar</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/testjar.jar</systemPath>
    </dependency>
于 2013-01-18T07:12:35.923 回答