我需要为我的客户配置并在他们的网站上运行一个简单的应用程序。我正在使用Spring框架,所以我有许多必须在类路径上的配置文件。我使用Maven2和Netbeans作为我的IDE。
我能够使用Netbeans / Maven创建和运行我的应用程序,并且我正在使用Application Assembler Maven插件来生成可运行的应用程序。除了我的Spring配置文件必须放在 src / main / resources 中,这意味着它们被打包到生成的JAR文件中,所有这些都可以正常工作。
我需要我的客户能够修改配置文件以进行测试,但要求他们修改JAR中打包的副本是不合理的。
可能有许多解决方案,但在我看来,最简单的方法是让Maven根本不将应用程序和配置文件打包到JAR中,只需将它们留在类似类的类中可以运行它们的目录。这将允许用户轻松修改配置文件。不幸的是,我无法弄清楚如何让Maven以这种方式“打包”应用程序,或者如何让AppAssembler生成生成的runnable。
以下是我的pom.xml的摘录,可能有助于说明我要做的事情:
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
... stuff deleted ...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<!-- Set the target configuration directory to be used in the bin scripts -->
<configurationDirectory>conf</configurationDirectory>
<!-- Copy the contents from "/src/main/config" to the target
configuration directory in the assembled application -->
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<!-- Include the target configuration directory in the beginning of
the classpath declaration in the bin scripts -->
<includeConfigurationDirectoryInClasspath>
true
</includeConfigurationDirectoryInClasspath>
<platforms>
<platform>windows</platform>
</platforms>
<programs>
<program>
<mainClass>org.my.path.App</mainClass>
<name>app</name>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
...
答案 0 :(得分:6)
单个打包的jar文件或一堆解压缩的类文件都不是专业客户端交付的良好格式。看看那些精彩的apache应用程序,如tomcat,ant和maven,它们作为tar.gz或zip文件提供,下载后,只需提取它们,你就会得到一个漂亮而干净的目录结构:
conf - &gt;将配置文件放在* .properties,logback.xml这里 doc - &gt; readme.txt,userguide.doc等 lib - &gt;把你的core.jar和依赖jar文件放在这里 run.bat - &gt;运行Windows的脚本
run.sh - &gt;运行Unix的脚本
我们也可以用Maven做这些事情。请注意,您应该设计并实现核心jar,以便正确地从conf目录中读取* .properties。然后使用maven-assembly-plugin将app打包到这个经典的目录结构中。
命令行应用程序的示例pom.xml:
<!-- Pack executable jar, dependencies and other resource into tar.gz -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>attached</goal></goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/main/assembly/binary-deployment.xml</descriptor>
</descriptors>
</configuration>
</plugin>
命令行应用的示例binary-deployment.xml:
<!--
release package directory structure:
*.tar.gz
conf
*.xml
*.properties
lib
application jar
third party jar dependencies
run.sh
run.bat
-->
<assembly>
<id>bin</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/java</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory></outputDirectory>
<filtered>true</filtered>
<fileMode>755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/doc</directory>
<outputDirectory>doc</outputDirectory>
<filtered>true</filtered>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
答案 1 :(得分:4)
如果没有误导性,我想你想让jar和config分开,暴露jar以供客户测试。 以下可以使用copy-maven-plugin为您完成此任务,它可以完成assembly-plugin将执行的几乎任务,例如:复制,依赖等等 - 下载,上传,移动...... ....
<plugin>
<groupId>com.github.goldin</groupId>
<artifactId>copy-maven-plugin</artifactId>
<version>0.2.5</version>
<executions>
<execution>
<id>create-archive</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<resources>
<!--copy your scripts to ${myOutPutPath}/bin-->
<resource>
<targetPath>${myOutPutPath}/bin</targetPath>
<directory>${project.basedir}/src/main/scripts</directory>
<includes>
<include>*</include>
</includes>
</resource>
<resource>
<!--copy your configs-->
<targetPath>${myOutPutPath}/conf</targetPath>
<directory>${project.basedir}/src/main/config</directory>
<include>*</include>
</resource>
</resources>
</configuration>
</plugin>
打包主jar并放入$ {myOutPutPath}
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<!-- The configuration of the plugin -->
<configuration>
<outputDirectory>${myOutPutPath}</outputDirectory>
<!-- Configuration of the archiver -->
<archive>
<!-- Manifest specific configuration -->
<manifest>
<!-- Classpath is added to the manifest of the created jar file. -->
<addClasspath>true</addClasspath>
<!--
Configures the classpath prefix. This configuration option is
used to specify that all needed libraries are found under lib/
directory.
-->
<classpathPrefix>lib/</classpathPrefix>
<!-- Specifies the main class of the application -->
<mainClass>com.xinguard.snmp.SNMP_ETL</mainClass>
</manifest>
<!-- you need to add some classpath by yourself, like conf here for client to use-->
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
然后将lib jar打包到jar目录下的lib目录中。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${myOutPutPath}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>