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


先把解決方法搬出來

第一次是通過更新插件版本(apt-maven-plugin):<version>1.1.3</version>

第二次是通過將除了Q對象不存在之外的編譯錯誤全部處理后解決的


以下是解決過程:

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

查到原因在於QueryDSL類沒有生成,但為何在公司可以而在家里就不行呢?

鑒於Idea的“Generate Sources And Update Folders”操作一閃即過,信息太少,所以不得先從原理上追溯

 

1. 首先的疑問是:當執行Idea的“Generate Sources And Update Folders”操作時,都發生了什么?

  參考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 generate-sources Maven phase for any plug-ins in your pom.xml that do generate any sources.
“Generate Source”實際上是用所有可以生成source的插件執行Maven的generate-sources步驟

這里需要了解的是Maven的phase都有哪些?generate-sources是什么時機執行的?

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

 

2. 那么第二個問題來了,我們的項目中哪些plugin可以執行generate sources

     很容易找到下面的配置(此插件開源在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

https://www.cnblogs.com/fortitude/p/10936386.html

QueryDSL和JPA的配合

https://www.cnblogs.com/chenglc/p/11230755.html

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM