在制作项目罐时包括一些罐子并排除其他罐子?

时间:2013-01-18 07:01:32

标签: java maven pom.xml

我正在使用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 个答案:

答案 0 :(得分:0)

provide来自provided

  

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

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

答案 1 :(得分: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>

答案 2 :(得分:0)

“项目罐子”中的第三方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>

答案 3 :(得分:0)

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

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

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

YourMainJar.jar是您拥有主要课程的文件,第三方广告系列需要位于YourMainJar.jar所在的同一位置。

当您双击YourMainJar.jar时,它会自动运行并从同一位置挑选您的第三方罐子。

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

相关问题