ronmamo / reflections

Java runtime metadata analysis
Do What The F*ck You Want To Public License
4.7k stars 698 forks source link

testing classes in reflection.json #310

Open asafbennatan opened 3 years ago

asafbennatan commented 3 years ago

Hi , i am generating the reflection info during compile time , following maven project structure conventions the test class for class x.y.z.SomeClass.java(src/main/java/x/y/z) is in x.y.z.SomeClassTest.java(src/test/java/x/y/z) , I notice that that gmaven plus along with reflection library causes the test class to be included inside the reflection.json this causes a class not found exception duration run time when iterating over the loaded classes (since these classes are not present in runtime). I have tried excluding these classes by using FilterBuilder.exclude giving /src/test/java/* but it did not work . the only workaround i found so far is to move the test class into x.y.z.test.SomeClassTest.java and exclude x.y.z.test package but this does not follow maven conventions ( this for example will prevent testing package level classes and methods). is there a room for implementing a filter excluding path(s) or am I missing something ? Thanks.

plugin:

<plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.10.1</version>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>

                    </execution>
                </executions>
                <configuration>
                    <scripts>

                        <script><![CDATA[
                           log.debug("The settings are " + project.properties['reflections'])
                            if(!new File( project.properties['reflections']).getParentFile().mkdirs()){
                            log.debug("Failed created dir at "+project.properties['reflections'])
                            }

                       new org.reflections.Reflections(new org.reflections.util.ConfigurationBuilder().filterInputsBy(new org.reflections.util.FilterBuilder().exclude("^com.flexicore.test.*")).setUrls(org.reflections.util.ClasspathHelper.forPackage("com.flexicore"))
                    .setScanners(new org.reflections.scanners.TypeAnnotationsScanner(), new org.reflections.scanners.SubTypesScanner(false), new org.reflections.scanners.MethodAnnotationsScanner()))
                            .save(project.properties['reflections'],new org.reflections.serializers.JsonSerializer())

                    ]]></script>
                    </scripts>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.reflections</groupId>
                        <artifactId>reflections</artifactId>
                        <!-- use latest version of Reflections -->
                        <version>0.9.11</version>
                    </dependency>
                    <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <!-- any version of Groovy \>= 1.5.0 should work here -->
                        <version>2.4.3</version>
                        <scope>runtime</scope>
                    </dependency>

                </dependencies>
            </plugin>
Michael1993 commented 3 years ago

Have you tried excluding your test directory in the gmavenplus-plugin configuration?

...
<configuration>
  <excludes>**/test/*</excludes>
</configuration>
...

Not sure about the actual syntax (this is based on the Maven JAR plugin).

I'd also give this configuration a try (if it does not take more than 5 minutes): https://stackoverflow.com/questions/50033201/tell-gmaven-plugin-to-skip-exclude-certain-files

asafbennatan commented 3 years ago

hi @Michael1993 thanks for the response. my ide is complaining on the the syntax you wrote above and it doesn't work. tried the one from the link you have sent but still it includes the annotations from the test classes , here is what i tried:

<plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.10.1</version>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>

                    </execution>
                </executions>
                <configuration>
                    <sources>
                        <source>
                            <directory>${project.basedir}/src/main</directory>
                        </source>
                    </sources>
                    <scripts>

                        <script><![CDATA[
                           log.debug("The settings are " + project.properties['reflections'])
                            if(!new File( project.properties['reflections']).getParentFile().mkdirs()){
                            log.debug("Failed created dir at "+project.properties['reflections'])
                            }

                       new org.reflections.Reflections(new org.reflections.util.ConfigurationBuilder().filterInputsBy(new org.reflections.util.FilterBuilder().exclude("^com.flexicore.test.*")).setUrls(org.reflections.util.ClasspathHelper.forPackage("com.flexicore"))
                    .setScanners(new org.reflections.scanners.TypeAnnotationsScanner(), new org.reflections.scanners.SubTypesScanner(false), new org.reflections.scanners.MethodAnnotationsScanner()))
                            .save(project.properties['reflections'],new org.reflections.serializers.JsonSerializer())

                    ]]></script>
                    </scripts>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.reflections</groupId>
                        <artifactId>reflections</artifactId>
                        <!-- use latest version of Reflections -->
                        <version>0.9.11</version>
                    </dependency>
                    <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <!-- any version of Groovy \>= 1.5.0 should work here -->
                        <version>2.4.3</version>
                        <scope>runtime</scope>
                    </dependency>

                </dependencies>
            </plugin>

also this did not work:

  <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.10.1</version>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>

                    </execution>
                </executions>
                <configuration>
                    <sources>
                        <source>
                            <directory>${project.basedir}/src/main</directory>
                            <excludes>
                                <exclude>**/test/**</exclude>
                            </excludes>
                        </source>
                    </sources>
                    <scripts>

                        <script><![CDATA[
                           log.debug("The settings are " + project.properties['reflections'])
                            if(!new File( project.properties['reflections']).getParentFile().mkdirs()){
                            log.debug("Failed created dir at "+project.properties['reflections'])
                            }

                       new org.reflections.Reflections(new org.reflections.util.ConfigurationBuilder().filterInputsBy(new org.reflections.util.FilterBuilder().exclude("^com.flexicore.test.*")).setUrls(org.reflections.util.ClasspathHelper.forPackage("com.flexicore"))
                    .setScanners(new org.reflections.scanners.TypeAnnotationsScanner(), new org.reflections.scanners.SubTypesScanner(false), new org.reflections.scanners.MethodAnnotationsScanner()))
                            .save(project.properties['reflections'],new org.reflections.serializers.JsonSerializer())

                    ]]></script>
                    </scripts>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.reflections</groupId>
                        <artifactId>reflections</artifactId>
                        <!-- use latest version of Reflections -->
                        <version>0.9.11</version>
                    </dependency>
                    <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <!-- any version of Groovy \>= 1.5.0 should work here -->
                        <version>2.4.3</version>
                        <scope>runtime</scope>
                    </dependency>

                </dependencies>
            </plugin>