我需要为我的客户生成一个简单的应用程序,并在他们的网站上运行.我正在使用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>
...
Run Code Online (Sandbox Code Playgroud)
单个打包的jar文件或一堆解压缩的类文件都不是专业客户端交付的良好格式.看看那些精彩的apache应用程序,如tomcat,ant和maven,它们作为tar.gz或zip文件提供,下载后,只需提取它们,你就会得到一个漂亮而干净的目录结构:
conf - >把配置文件像*.properties,logback.xml这里
doc - > readme.txt,userguide.doc等
lib - >把你的core.jar与依赖jar文件放在这里
run.bat - >运行脚本对于Windows
run.sh - >运行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>
Run Code Online (Sandbox Code Playgroud)
示例命令行应用程序的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>
Run Code Online (Sandbox Code Playgroud)