解決Idea的Generate Sources沒法生成QueryDSL問題

今天是2020年第一天在家辦公,就出現了跟在公司不同的現象,deploy項目到maven庫時失敗,以前一直成功。html

查到緣由在於QueryDSL類沒有生成,但爲什麼在公司能夠而在家裏就不行呢?java

鑑於Idea的「Generate Sources And Update Folders」操做一閃即過,信息太少,因此不得先從原理上追溯git

 

1. 首先的疑問是:當執行Idea的「Generate Sources And Update Folders」操做時,都發生了什麼?github

  參考stackoverflow,解釋以下  安全

In order to get generated sources automatically imported as source folders configure corresponding plugins 
so that they put them into target/generated-sources/, where subdir is any folder name you prefer.
The subdir folder is necessary to distinguish sources from different tools and also to exclude some special generated sources (e.g. groovy stubs).
Please note that even if you manually configure some source folders under target/generated-sources of this folder itself,
IDEA will rewrite them according to your pom.xml. Any time you want to generate sources you simply execute the corresponding goal,
bound for generation (usually generate-sources, generate-test-sources). After that IDEA will pick up new folders and set them up.

As you can see Generate Sources action runs the  Maven phase for any plug-ins in your  that do generate any sources.
「Generate Source」其實是用全部能夠生成source的插件執行Maven的generate-sources步驟
generate-sourcespom.xml

這裏須要瞭解的是Maven的phase都有哪些?generate-sources是什麼時機執行的?框架

答案是generates階段會在validate和compile階段之間執行,詳細可參考這裏maven

 

2. 那麼第二個問題來了,咱們的項目中哪些plugin能夠執行generate sourceside

     很容易找到下面的配置(此插件開源在github上工具

            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>4.1.3</version>
                    </dependency>

  github的解釋很簡單:apt-maven-plugin provides Maven integration of the Java 6 APT functionality.性能

  這裏有必要了解下什麼是Java APT?

APT(Annotation Process Tool),是一種在代碼編譯時處理註解,按照必定的規則,生成相應的java文件,多用於對自定義註解的處理,
目前比較流行的Dagger2, ButterKnife, EventBus3都是採用APT技術,對運行時的性能影響很小
也就是說,APT是用代碼生成代碼的工具,會在process過程生成java文件,那麼爲何咱們最終生成的每每只有class文件呢?這是由於不少插件都作了第二步的清理操做。
至於Java8以後APT被「"Pluggable Annotation Processing API".」替換,那就是後話了
 

  另外,此插件依賴querydsl,因此querydsl也有必要了解下

QueryDSL僅僅是一個通用的查詢框架,專一於經過Java API構建類型安全的SQL查詢。藉助QueryDSL能夠在任何支持的ORM框架或者SQL平臺上以一種通用的API方式來構建查詢。
目前QueryDSL支持的平臺包括JPA,JDO,SQL,Java Collections,RDF,Lucene,Hibernate Search。
因此說咱們項目中所用的QueryDSL是在JPA之上的,是爲了補充JPA的複雜查詢支持不足而引入的

 

3. 那麼如何手動單獨執行此APT的process呢?

    這樣考慮的目的其實就是爲了獲得更多信息,此步驟能夠用Idea的此選項右鍵執行,或者在command中執行「mvn apt:process

   

 

  會發現輸出log中輸出如下警告

'build.plugins.plugin.version' for com.mysema.maven:apt-maven-plugin is missing. @ line 46, column 21

  因而就在pom配置中添加plugin的最新version

<version>1.1.3</version>

再次generate,生成成功!

 

經過解決此問題獲得一點感觸:每一次出現問題很差解決時,嘗試從原理層面作一個快速全面的瞭解,這樣不單會有助於使本身對於技術「知其因此然」,並且會反過來觸發解決問題的新思路。

 

參考:

https://stackoverflow.com/questions/54868822/generate-sources-and-update-folders-for-all-projects

https://www.runoob.com/maven/maven-build-life-cycle.html

https://github.com/querydsl/apt-maven-plugin/

https://blog.csdn.net/fengxingzhe001/article/details/78520298

http://www.javashuo.com/article/p-rpbfxgky-dr.html

QueryDSL和JPA的配合

http://www.javashuo.com/article/p-oxaslwhs-dy.html

https://zhuanlan.zhihu.com/p/24778422

相關文章
相關標籤/搜索