您好我正在嘗試了解 Maven 中的過濾包含/排除,因為我是 Maven 菜鳥。我正在查找如何從 JAVA 中的 pom 檔案中讀取版本并找到了解決方案,但我對過濾有一些疑問。我了解在同一資源塊中使用包含和/或排除進行過濾,但我不明白當您在同一檔案/檔案夾的不同資源塊中包含/排除時會發生什么。
我的檔案夾結構與簡化的 RandomFolder 及其內容:
src
├── main
│ ├── resources
│ ├── RandomFolder
│ ├──aFile.dll
│ ├──someFile.txt
│ └── pom.properties
pom.properties 檔案包含:
version=${revision}
#1
這就是我目前將它(簡化)作為作業解決方案的方式。我知道它正在過濾 pom.properties 檔案并將 '${revision}' 替換為 '1.0.0' aka 'version=1.0.0'。運行時是否會過濾 pom.properties 檔案(又名“版本=1.0.0”)mvn package并生成 jar 檔案?我會這么認為,但想確定一下。
我也明白,通常你不會在過濾中指定檔案名,而是指定檔案名**/*.properties,但由于這是唯一的屬性檔案,我認為擁有檔案名更容易和更清晰,它不應該引起任何問題(讓我知道我在這個假設上是否錯了)。
...
<properties>
<revision>1.0.0</revision>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
</resource>
</resources>
...
</build>
#2
我相信這是教程網站通常表示資源塊中包含和排除的方式。在這個例子中真的沒有必要,因為它過濾除了 pom.properties 之外的所有內容(發現并測驗了 excludes 塊覆寫 include 塊,無論順序如何)。
這里沒有問題。只是想說我知道這種方法。
...
<properties>
<revision>1.0.0</revision>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
<excludes>
<exclude>**/pom.properties</exclude>
</excludes>
</resource>
</resources>
...
</build>
#3
為什么這行得通?我會假設它首先過濾檔案,以便檔案讀取“1.0.0”然后不過濾,但我想確定。這源于我發現獲得 pom 版本的類似解決方案(請參閱下面的 #5)。我對此進行了測驗,它過濾了 pom.properties 檔案。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/pom.properties</exclude>
</excludes>
</resource>
</resources>
...
</build>
#4
The flip flop of #2 with the exclude block before the include block, which seems kinda unnecessary. I would assume its not filtering the file first then filtering the file so that the file reads '1.0.0', but I want to make sure. I tested this and it filters the pom.properties file.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/pom.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
</resource>
</resources>
...
</build>
#5
This I don't really know. This was a top solution found for: Maven resource filtering exclude
And the link provided in that solution I feel doesn't explain the reasoning for its' solution: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
So is it filtering everything except the pom.properties file but then not filter everything including the pom.properties file? Why? What's the purpose of this? Are they both the same thing? Or a double negative? Does it impact what appears in the build/package?
This 'solution' did not filter the pom.properties file when I tested it.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/pom.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
</resource>
</resources>
...
</build>
Please let me know if you need additional information. I would really appreciate it if you can go through each section (#1 to #5) and answer them. Thank you in advance!
Solution
Robert Scholte's response helped me understand that filtering also copied over files. This is something I did not understand before as I thought it was just modifying the file(s) in the desired folder(s).
With additional help with understanding exclusions from: http://www.avajava.com/tutorials/lessons/how-do-i-exclude-particular-resources-from-being-processed.html
I was able to answer my questions.
#1. Still not sure, but I believe the pom.properties file would be filtered in jar.
#2. N/A. No question to answer.
#3. For #3, #4, #5 I created a test.properties in same folder as pom.properties and contains the same contents.
Testing required me to use command: mvn clean install -DskipTests to visually see the differences between #3, #4 and #5.
This would be the result after creating the test.properties file and running the above command (without the block in pom file) for a visual reference:
src
├── main
│ ├── resources
│ ├── RandomFolder
│ ├──aFile.dll
│ ├──someFile.txt
│ └── pom.properties
target
├── classes
│ ├── MultipleGeneratedFolders
│ ├── RandomFolder
│ ├──aFile.dll
│ ├──someFile.txt
│ ├── pom.properties
│ └── test.properties
So for #3, it filtered only the pom.properties and moved to a '/target/classes/' path (same level as 'src'). But because of the second resource block, it copies over all the contents in the RandomFolder and the test.properties file to '/target/classes/' path without filtering them!
#4. Exact same result as #3. Everything is copied over '/target/classes/', but only pom.properties file is filtered.
#5. Here is the interesting one. The first resource block with filtering true and excluding pom.properties is filtering everything (since there is no , it defaults to everything) and then copies to '/target/classes/' path. So this means the test.properties file gets filtered! The second resource block with filtering false and including pom.properties is not filtering the pom.properties file (so it leaves as raw text) then copies over to '/target/classes/' path. Basically, everything besides pom.properties gets filtered and copied over to '/target/classes/' path.
Summary of Conclusions
In short, filtering refers to replacing the variables in files with the properties block in the pom file, such as ${revision}. Filtering does not require or (if neither are specified it defaults to including all files) and copies selected files to the target folder.
If you filter (set to TRUE) and only a specific pattern or file, it will only filter the selected files and copy them to the target folder. If you filter (set to TRUE) and only , it will filter everything except for the specified pattern or file and copy only the filtered files to the target folder.
If you don't filter (set to FALSE) and only a specific pattern or file, it will only copy the selected files to the target folder without filtering them. If you don't filter (set to FALSE) and only a specific pattern or file, it will copy all files except the selected files to the target folder without filtering them.
(I could be wrong in my conclusions so take it may not be completely accurate, but 'good enough for government work' as they say)
uj5u.com熱心網友回復:
您可以運行mvn process-resources -X以查看有關復制哪些檔案的更多詳細資訊。
但這就是它的作業原理:每個資源塊通過選擇全部includes(默認為全部)減去全部excludes(默認為無)來選擇要復制的檔案集。如果過濾設定為 true,則這些檔案中的占位符將被替換,否則將按原樣復制。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/410277.html
標籤:
上一篇:如何使用指向“\0”的第一個元素和屬性的指標獲取陣列的大小?[復制]
下一篇:從log4j遷移到log4j2
