【问题标题】:Include Some jars while making a project jar and exclude others?在制作项目jar时包含一些jar并排除其他jar?
【发布时间】:2013-01-18 16:25:06
【问题描述】:

我将 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。我在哪里弄错了,它不能包含任何罐子?

【问题讨论】:

    标签: java maven pom.xml


    【解决方案1】:

    根据您的上述 cmets,我假设您有一个 jar,其中包含您的主类。

    在您的项目中添加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 时,它会自动运行并从同一位置选择您的第三方 jar。

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

    【讨论】:

    • 这个有效。只是想知道,有什么方法可以通过 Maven 做到这一点。就像,在 pom.xml 中指定一些东西,以便它包含我的一些第三方引用的 jar 和我的项目 jar。?我进行了搜索,可以通过创建项目的程序集 jar 来包含所有第三方引用的 jar。然而,没有发现任何关于只包括一些提到的罐子的东西。???。 .
    • 你可能想看看这个答案......LINK
    【解决方案2】:

    maven docsprovide

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

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

    【讨论】:

    • 是的,我使用了为我不想在项目 jar 中使用的库提供的范围。并且在制作项目 jar 时,我没有使用 scope-provided 作为我想要包含在我的 jar 中的依赖项。
    【解决方案3】:

    删除范围标签。

    如果仍有问题,请使用我的工作 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>
    

    【讨论】:

      【解决方案4】:

      “项目 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>
      

      【讨论】:

      • 只需添加依赖项不会将那些第三方 jar 包含到我的 jar 中。因此,如果我在其他地方运行我的项目 jar,它将无法找到第三方 jar 中的那些库。谢谢。
      • maven 用于构建项目而不是运行项目。如果您在运行项目时需要 jar,则将它们添加到项目的类路径中。如果您将项目打包在 jar 中,则将您的 jar 添加到清单文件中。
      • 我接受你的观点。我只是说如果您想制作一个包含项目引用的一些(而不是全部)库的捆绑 jar,应该怎么做?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2017-04-07
      • 2011-04-11
      • 1970-01-01
      • 2014-03-28
      相关资源
      最近更新 更多