在maven项目中包含xml文件

时间:2016-09-12 17:19:16

标签: maven maven-plugin

我有一个maven pom文件,可以从以下结构构建:

包1
--1.java
--2.java
--packageMetaInfo.xml

包2
--21.java
--22.java
--packageMetaInfo.xml

当我进行maven编译时,xml文件不会进入目标 maven-compiler-plugin 3.5.1 - 除非我通过<exclusions>排除xmls,否则会收到“致命错误编译:所有编译单元必须属于SOURCE类 - &gt;”的错误。 maven-compiler-plugin 2.0.1 - 编译但跳过xmls

有没有办法让我的jar中包含xmls。结构将是

x.jar
包1
--1.class
--2.class
--packageMetaInfo.xml

包2
--21.class
--22.class
--packageMetaInfo.xml

*我理解将xml与源文件放在一起可能不是标准,但是我正在处理特定的产品,需要在输入和输出中维护这个结构。

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.incent</groupId>
  <artifactId>Release-4.3.0.1.0.0001</artifactId>
  <name>AAA Custom code</name>
  <version>2.4.0.1</version>  

  <properties>
        <ormb.cmccb.path>./Active_Repository/CMCCB</ormb.cmccb.path>
        <ormb.customcode.path>${ormb.cmccb.path}/data</ormb.customcode.path>
        <ormb.release.name>AAA-4.3.0.1.0.0001</ormb.release.name>
        <ormb.target.path>./target</ormb.target.path>
        <ormb.output.path>Release-${ormb.release.name}/Application/${ormb.release.name}/CMCCB</ormb.output.path>
        <ormb.serverfile.output.relpath>./target/server</ormb.serverfile.output.relpath>    
    <build.number>SNAPSHOT</build.number>
  </properties>



 <dependencies>

    <dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm</artifactId>
    <version>5.0.3</version>
</dependency>

    <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>2.7.7</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>icu4j</groupId>
        <artifactId>icu4j</artifactId>
        <version>49.1</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.2</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>wlfullclient</groupId>
        <artifactId>wlfullclient</artifactId>
        <version>10.3.4.0</version>
        <type>jar</type>
    </dependency>
    </dependencies>


  <build>

    <sourceDirectory>${ormb.customcode.path}/java</sourceDirectory>  

    <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <encoding>UTF-8</encoding>
        </configuration>
        <executions>
           <execution>
            <id>copy-cm</id>
            <phase>install</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${ormb.customcode.path}/etc/lib</outputDirectory>
                <overwrite>true</overwrite>
                <resources>
                    <resource>
                        <directory>${ormb.target.path}</directory>
                        <includes>
                            <include>cm.jar</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
            </execution>
            <execution>
                <id>copy-mwpackage</id>
                <phase>install</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>${ormb.serverfile.output.relpath}</outputDirectory>
                <overwrite>true</overwrite>
                <resources>
                    <resource>
                        <directory>${ormb.target.path}</directory>
                        <includes>
                            <include>Release-${ormb.release.name}.zip</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
            </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <encoding>ISO-8859-1</encoding> 
            <source>1.7</source>
            <target>1.7</target>
          <includes>
            <include>**/cm/**</include> 
          </includes>
          <excludes>
            <exclude>**/*.xml</exclude>
          </excludes>
        <resources>
            <resource>
            <directory>${ormb.customcode.path}/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            </resource>
        </resources>


        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
         <executions>
         <execution>
            <id>create-cm</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>Active_Repository/assembly/executable.xml</descriptor>
                </descriptors>
                <finalName>cm</finalName>
                 <manifest>
                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                </manifest>
                <archive>
                    <manifestEntries>
                        <Specification-Title>${project.name}</Specification-Title>
                        <Specification-Version>${project.version}</Specification-Version>
                        <Implementation-Version>${build.number}</Implementation-Version>
                    </manifestEntries>
                </archive>
            </configuration>
            </execution>
            <execution>
            <id>create-distro</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>Active_Repository/assembly/dist.xml</descriptor>
              </descriptors>
              <finalName>custom-action-dist</finalName>
              <appendAssemblyId>false</appendAssemblyId>
              <finalName>Release-${ormb.release.name}</finalName>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

2 个答案:

答案 0 :(得分:4)

我遇到同样的问题,我想保留fxml&amp;同一文件夹中的java文件(我使用的是同时需要fxml和java文件的scenebuilder)。

这是我在pom.xml中的解决方案:

<resources>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
    <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.fxml</include>
        </includes>
  </resource>        
</resources>    

答案 1 :(得分:2)

如果您希望将xml文件包含在JAR文件中,请将它们存储在resources文件夹中,而不是java。

据推测他们在src / main / java中。尝试将它们移动到src / main / resources。 src / main / resources中的任何内容都将与.class文件一起打包到JAR文件中。