1. 程式人生 > 程式設計 >Idea的Generate Sources無法生成QueryDSL問題及解決方法

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

QueryDSL簡介

官網

1 QueryDSL僅僅是一個通用的查詢框架,專注於通過Java API構建型別安全的SQL查詢。

2 Querydsl可以通過一組通用的查詢API為使用者構建出適合不同型別ORM框架或者是SQL的查詢語句,也就是說QueryDSL是基於各種ORM框架以及SQL之上的一個通用的查詢框架。

3 藉助QueryDSL可以在任何支援的ORM框架或者SQL平臺上以一種通用的API方式來構建查詢。目前QueryDSL支援的平臺包括JPA,JDO,SQL,Java Collections,RDF,Lucene,Hibernate Search。

今天是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,Hibernate Search。

所以說我們專案中所用的QueryDSL是在JPA之上的,是為了補充JPA的複雜查詢支援不足而引入的

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

這樣考慮的目的其實就是為了得到更多資訊,此步驟可以用Idea的此選項右鍵執行,或者在command中執行“mvn apt:process”

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

會發現輸出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,生成成功!

通過解決此問題得到一點感觸:每一次出現問題不好解決時,嘗試從原理層面做一個快速全面的瞭解,這樣不單會有助於使自己對於技術“知其所以然”,而且會反過來觸發解決問題的新思路。

總結

以上所述是小編給大家介紹的Idea的Generate Sources無法生成QueryDSL問題及解決方法,希望對大家有所幫助!