ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

解决Idea的Generate Sources无法生成QueryDSL问题

2020-02-03 23:00:36  阅读:666  来源: 互联网

标签:sources QueryDSL apt Idea maven Sources APT com generate


今天是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

标签:sources,QueryDSL,apt,Idea,maven,Sources,APT,com,generate
来源: https://www.cnblogs.com/roostinghawk/p/12257785.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有