Maven 2 使用Maven创建一个带有用户可访问配置文件的简单应用程序

Maven 2 使用Maven创建一个带有用户可访问配置文件的简单应用程序,maven-2,Maven 2,我需要为我的客户制作一个简单的应用程序,并在他们的网站上配置和运行。我使用的是Spring框架,因此我有许多配置文件必须位于类路径上。我使用带Netbeans的Maven2作为我的IDE 我能够使用Netbeans/Maven创建和运行我的应用程序,并且我正在使用applicationassemblermaven插件生成可运行的应用程序。除了我的Spring配置文件必须放在src/main/resources中之外,所有这些都可以正常工作,这意味着它们被打包到结果JAR文件中 我需要我的客户能够

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

我能够使用Netbeans/Maven创建和运行我的应用程序,并且我正在使用applicationassemblermaven插件生成可运行的应用程序。除了我的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>
...
。。。
UTF-8
朱尼特
朱尼特
3.8.1
测试
org.springframework
spring上下文
3.1.0.1发布
... 删除的内容。。。
org.codehaus.mojo
appassembler maven插件
1.2
形态
真的
真的
窗户
org.my.path.App
应用程序
...

对于专业客户机交付来说,单个打包的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汇编插件将应用程序打包到这个经典的目录结构中

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

org.apache.maven.plugins
maven汇编插件
2.2-β-5
包裹
附属的
src/main/assembly/binary-deployment.xml
命令行应用程序的binary-deployment.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>
<!--
  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>

箱子
tar.gz
真的
src/main/java
形态
*.xml
*.物业
src/main/bin
真的
755
src/main/doc
医生
真的
解放党
真的
假的
运行时

如果没有误导的话,我认为您希望将jar和config分开,将jar公开给客户端测试。 以下内容可以使用copymaven插件为您实现这一点,它可以完成组装插件所能完成的几乎所有任务,例如:复制、依赖和更多-下载、上传、移动等等

        <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>

com.github.goldin
复制maven插件
0.2.5
创建存档
包裹
复制
${myOutPutPath}/bin
${project.basedir}/src/main/scripts
*
${myOutPutPath}/conf
${project.basedir}/src/main/config
*
打包主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>

org.apache.maven.plugins
maven jar插件
2.3.1
${myOutPutPath}
真的
解放党/
com.xinguard.snmp.snmp_ETL
形态/
然后将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>

org.apache.maven.plugins
maven依赖插件
复制依赖项
准备包装
复制依赖项
${myOutPutPath}/lib

谢谢您的帮助。我应该提到,我希望推出简单的测试应用程序,以便在客户环境中测试无法在开发环境中复制的东西。在本例中,我正在测试他们的Active Directory设置。所以我不需要做一个完全专业的部署包。我非常想自动创建