首页 新闻 会员 周边

maven build 发布时,关于设置生成jar包的classpath的一些问题

0
悬赏园豆:30 [已解决问题] 解决于 2016-03-15 11:24

背景:

使用maven管理项目,导出jar包,使用maven-jar-plugin插件

项目结构

.
├── pom.xml
├── README.TXT
├── src
│   ├── main
│   │   ├── assembly
│   │   ├── config
│   │   ├── java
│   │   ├── resources
│   │   └── scripts
│   └── test
│       ├── java
│       └── resources
└── target

pom.xml文件

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <!-- The configuration of the plugin -->
            <configuration>
                <!-- Configuration of the archiver -->
                <archive>

                    <!--
                        生成的jar中,不要包含pom.xml和pom.properties这两个文件
                    -->
                    <addMavenDescriptor>false</addMavenDescriptor>

                    <!-- Manifest specific configuration -->
                    <manifest>
                        <!--
                            是否要把第三方jar放到manifest的classpath中
                        -->
                        <addClasspath>true</addClasspath>
                        <!--
                           生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/
                       -->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--
                            应用的main class
                        -->
                        <mainClass>com.zzw.demo.App</mainClass>
                        
                        
                        
                    </manifest>
                </archive>
                <!--
                    过滤掉不希望包含在jar中的文件
                -->
                <excludes>
                    <exclude>${project.basedir}/xml/*</exclude>
                </excludes>
            </configuration>
        </plugin>

生成的jar包中的MANIFEST.MF 文件为

Manifest-Version: 1.0
Built-By: xxx
Build-Jdk: 1.7.0_79
Class-Path: lib/jfreechart-1.0.19.jar lib/jcommon-1.0.23.jar lib/mail-
 1.4.7.jar lib/activation-1.1.jar lib/httpclient-4.5.1.jar lib/httpcor
 e-4.4.3.jar lib/commons-logging-1.2.jar lib/commons-codec-1.9.jar lib
 /commons-io-2.4.jar lib/dom4j-1.6.1.jar lib/xml-apis-1.0.b2.jar
Created-By: Apache Maven 3.3.3
Main-Class: com.xxx.demo.App
Archiver-Version: Plexus Archiver

因为配置文件不放在jar包中,在jar外部的config文件夹中.目的是在MANIFEST.MF 中的Class-Path:后加 config/

pom.xml怎么写

(手动改重新打包可以,sh脚本也能做到,现在想通过maven功能导出jar的时候class-path: 已经存在了 config/ )

小风疏雨的主页 小风疏雨 | 初学一级 | 园豆:128
提问于:2016-03-11 19:40
< >
分享
最佳答案
0

走入误区了,一直以为在<manifest>标签里设置.

与他同级的<manifestEntries>标签也可以设置class-path的,而且是我想要的追加的效果

附上<build>节点内容

  <build>
    <plugins>
        <!-- The configuration of maven-jar-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <!-- The configuration of the plugin -->
            <configuration>
                <!-- Configuration of the archiver -->
                <archive>

                    <!--
                        生成的jar中,不要包含pom.xml和pom.properties这两个文件
                    -->
                    <addMavenDescriptor>false</addMavenDescriptor>

                    <!-- Manifest specific configuration -->
                    <manifest>
                        <!--
                            是否要把第三方jar放到manifest的classpath中
                        -->
                        <addClasspath>true</addClasspath>
                        <!--
                           生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/
                       -->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--
                            应用的main class
                        -->
                        <mainClass>com.xxx.demo.App</mainClass>
                        
                    </manifest>
                    <manifestEntries>
                        <Class-Path>config/</Class-Path>
                    </manifestEntries>
                    
                </archive>
                <!--
                    过滤掉不希望包含在jar中的文件
                -->
                <excludes>
                    <exclude>${project.basedir}/xml/*</exclude>
                </excludes>
            </configuration>
        </plugin>

        <!-- The configuration of maven-assembly-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <!-- The configuration of the plugin -->
            <configuration>
                <!-- Specifies the configuration file of the assembly plugin -->
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

最后,向google致敬

小风疏雨 | 初学一级 |园豆:128 | 2016-03-14 09:31
其他回答(1)
0

你的pom文件缺少了build标签和profiles标签   看看我的项目里的pom文件

<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>TestDao</groupId>
<artifactId>TestDao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.framework.version>4.0.6.RELEASE</spring.framework.version>
<spring.security.version>3.2.3.RELEASE</spring.security.version>
<jackson.version>2.2.3</jackson.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-rebel-xml</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8080</port>
<path>/lcm-ctlcenter</path>
<systemProperties>
<com.sun.management.jmxremote.port>4000</com.sun.management.jmxremote.port>
</systemProperties>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.zeroturnaround
</groupId>
<artifactId>
jrebel-maven-plugin
</artifactId>
<versionRange>
[1.0.7,)
</versionRange>
<goals>
<goal>generate</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>


<profiles>
<profile>
<id>buildExecutableWar</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<path>/</path>
</configuration>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/lcm-ctlcenter</path>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>


</project>

 

dependencies引入的包我就省略了

 

收获园豆:30
snowjake123 | 园豆:469 (菜鸟二级) | 2016-03-12 15:26

我的配置文件也有 只是截取了plugin这一个节点,后来google了一下找到解决方案了

支持(0) 反对(0) 小风疏雨 | 园豆:128 (初学一级) | 2016-03-14 09:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册