Gradle用jar中的类文件打包资源

时间:2015-10-29 13:59:03

标签: java maven gradle

maven pom在构建配置中有这个我们正在构建一个jar文件,它在类文件附近的包中有资源文件(这在我的情况下是必要的,因为一些类文件依赖于约定优于配置并期望这些资源文件是就在他们旁边)。

<build>
    ...
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.fxml</include>
                <include>**/*.css</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.css</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
    ...
</build>

我们如何用gradle做同样的事情?

1 个答案:

答案 0 :(得分:2)

您需要添加以下代码:

apply plugin: 'java'

jar {
   from('src/main/java') {
      include  '**/*.properties'
      include  '**/*.xml'
      include  '**/*.css'
   }
}

它的工作方式如下:

➜  33416293 git:(master) ✗ tree   
.
├── build.gradle
└── src
    └── main
        └── java
            └── lol
                ├── Lol.java
                ├── lol.css
                ├── lol.properties
                └── lol.xml

4 directories, 5 files
➜  33416293 git:(master) ✗ gradle jar   
:compileJava
:processResources UP-TO-DATE
:classes
:jar

BUILD SUCCESSFUL

Total time: 3.035 secs
➜  33416293 git:(master) ✗ jar -tvf build/libs/33416293.jar 
     0 Thu Oct 29 15:55:40 CET 2015 META-INF/
    25 Thu Oct 29 15:55:40 CET 2015 META-INF/MANIFEST.MF
     0 Thu Oct 29 15:55:40 CET 2015 lol/
   242 Thu Oct 29 15:55:40 CET 2015 lol/Lol.class
     8 Thu Oct 29 15:51:28 CET 2015 lol/lol.css
     9 Thu Oct 29 15:47:00 CET 2015 lol/lol.properties
    15 Thu Oct 29 15:51:22 CET 2015 lol/lol.xml
➜  33416293 git:(master) ✗ 

您可以在这里找到nla2bv_tactic