maven-2 - 使用 Maven 创建一个包含用户可访问的配置文件的简单应用程序

标签 maven-2

我需要为我的客户制作一个简单的应用程序,在他们的站点上配置和运行。我使用的是 Spring 框架,因此我有许多配置文件必须位于类路径上。我使用 Maven2 和 Netbeans 作为我的 IDE。

我能够使用 Netbeans/Maven 创建并运行我的应用程序,并且我正在使用应用程序组装器 Maven 插件来生成可运行的应用程序。所有这些工作正常,除了我的 Spring 配置文件必须放置在 src/main/resources 中,这意味着它们被打包到生成的 JAR 文件中。

我需要我的客户能够修改配置文件来进行测试,但要求他们修改 JAR 中打包的副本是不合理的。

也许有很多解决方案,但在我看来,最简单的方法是让 Maven 根本不将应用程序和配置文件打包到 JAR 中,而只是将它们留在类中 可以从中运行它们的目录。这将允许用户轻松修改配置文件。不幸的是,我不知道如何让 Maven 以这种方式“打包”应用程序,或者如何让 AppAssembler 生成生成的可运行文件。

这是我的 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>
...

最佳答案

单个打包的 jar 文件或一堆解压的类文件都不是专业客户交付的良好格式。看看那些出色的 apache 应用程序,如 tomcat、ant 和 maven,它们以 tar.gz 或 zip 文件形式提供,下载后,只需解压它们,您将获得一个漂亮且干净的目录结构:

conf --> put config file like *.properties, logback.xml here
doc --> readme.txt, userguide.doc etc
lib --> put you core.jar with dependency jar file here
run.bat --> run script for Windows
run.sh --> run script for Unix

我们也可以使用 Maven 来做这些事情。请注意,您应该设计和实现核心 jar 以正确从 conf 目录读取 *.properties。然后使用 maven-assemble-plugin 将您的应用程序打包到这个经典的目录结构中。

命令行应用程序的示例 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>

关于maven-2 - 使用 Maven 创建一个包含用户可访问的配置文件的简单应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9408184/

相关文章:

maven-2 - Maven-替换Jar中的文件

github - hudson.plugins.git.GitException : Could not fetch from any repository

maven - 如何将maven项目版本放入日志文件名(log4j.xml)

java - 创建 Java 服务或守护程序脚本的最佳实践

maven-2 - 当maven-antrun-plugin失败时,Maven构建失败

java - Maven Cobertura : unit test failed but build success

maven 原型(prototype) :generate failure caused by org. apache.maven.plugin.MojoFailureException

java - 生成包含 Cobertura 报告的 Maven 站点

java - Maven 站点部署失败,SCP 连接被拒绝 - Windows

Maven 用于多个模块