解决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