I'm using the Maven Android plugin to build my Android library using the apklib packaging. This produces an apklib archive of the library, so I'm also using the Maven jar plugin to produce a jar artifact that can be used in an app.
My problem is that BuildConfig.class and R.class are being included in the jar, which I don't want (really I would like to exclude anything in gen altogether). I've been trying to create exclude rules for them but haven't had any success. Here's the jar plugin configuration I've been using in my pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>**/BuildConfig.java</exclude>
<exclude>**/R.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
Any help is appreciated.
The trick here is to apply your configuration to the default-jar phase of the build lifecycle and exclude .class files, rather than .java files. You do this by adding <id>default-jar</id> to your execution as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>**/BuildConfig.class</exclude>
<exclude>**/R.class</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
You'll probably also want to exclude the classes R$attr.class, R$drawable.class, R$layout.class, and R$string.class.
Related
I have created a multi-module project in maven which is as follows:
root/pom.xml
|________client/pom.xml
|________/src/main/java
|________/src/main/resources
|________common/pom.xml
|________/src/main/java
|________tools/pom.xml
|________/src/main/java
|________server/pom.xml
|________/src/main/java
|________/src/main/resources
I would like to compile the "client" module code which depends on ALL java classes in "common" module but on SOME java classes in the "tools" module.
Using the build-helper-maven-plugin as below, I was able to add all java source files under common/ path, but I need a way to define individual java files as sources which are under tools/.
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../common/src/main/java</source>
<!-- Adding single java files as below does not work -->
<source>../tools/log/Log.java</source>
<source>../tools/socket/SocketClient.java</source>
<!----------------------------------------------------->
</sources>
</configuration>
</execution>
</executions>
You should include module "common" and "tools" as dependency in "client"
Then you can run build in the root of project: mvn clean install - it will build all modules.
Don't try to use parts of a module as dependency. Maven does not support this and it results in brittle and hard to maintain constructions.
If you only need parts of tools, then this is a clear signal that tools is too large and you need to split it up into two modules.
Ok, based on the comments received it seems that the ideal solution is to break the modules into sub-modules and use those as dependencies. However, as this is currently a time consuming task, I am posting a quick and dirty solution on how you can include one or more java files into your build by copying the files under a new source directory, src/main/external, and then add this directory as part of your sources.
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-source-java-files</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/external</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<!-- External source directory -->
<directory>../tools/src/main/java/</directory>
<includes>
<!-- Files to be copied along with their directory paths -->
<include>tools/log/Log.java</include>
<include>tools/socket/SocketClient.java</include>
<include>tools/client/reader/**</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
<source>src/main/resources</source>
<source>../common/src/main/java</source>
<!-- Here we add the source with the files copied -->
<source>src/main/external</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The output after compilation would be as follows:
root/pom.xml
|________client/pom.xml
|________/src/main/java
|________/src/main/resources
|________/src/main/external <----- This contains all external java files.
I am using Apache Maven Checkstyle plugin in my pom.xml.
I am trying to exclude the target directory from the check style scan but no luck so far. Here is the pom code i am trying.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<id>checkstyle-check</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>checkstyles.xml</configLocation>
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
<consoleOutput>true</consoleOutput>
<includes>**\/*.java,**\/*.groovy</includes>
<excludes>**WHAT GOES HERE TO EXCLUDE THE TARGET DIRECTORY**</excludes>
</configuration>
</plugin>
In Apache Maven Checkstyle Plugin version 3 to specify the location of the source directories we have to use sourceDirectories parameter. Then we can specify only directories of application/library and test sources to be used for Checkstyle:
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
</sourceDirectories>
Now only src/main/java and src/test/java will be analysed.
Here is my full working example:
<!-- Apache Maven Checkstyle Plugin (checks Java code adheres to a coding standard) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin.version}</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
</sourceDirectories>
<!-- relates to https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml -->
<configLocation>/src/main/resources/checkstyle.xml</configLocation>
</configuration>
</plugin>
<excludes>**/generated/**/*</excludes>
This will remove the generated files from the plugin.
I am generating a complete maven project (with its own pom.xml) with swagger codegen maven plugin. It outputs the project to generated-sources/swagger/ directory. However java sources in this directory are compiled against dependencies that are residing in my generator project's pom.xml, not against the one which is generated.
Is such configuration possible? I have already read about maven antlr4 and build helper plugins, but they do not seem useful for this purpose.
Use openapi-generator-maven-plugin to generate the source. Than the maven-invoker-plugin to build and test the generated source.
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>swagger.yaml</inputSpec>
<generatorName>java</generatorName>
<skipValidateSpec>true</skipValidateSpec>
<output>${project.build.directory}/generated-sources/openapi</output>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>${maven-invoker-plugin.version}</version>
<configuration>
<pom>${project.build.directory}/generated-sources/openapi/pom.xml</pom>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
In my java project I have generated classes which are inside the same package folder as the other classes. I would like to configure jacoco maven plugin to exclude those generated classes and only use classes in the main/src/java folder (not src/main/java-generated)
Project structure:
src/main/java/com/company/john/Good.java <---- this include
src/main/java-generated/com/company/john/AutoGeneratedClass.java <---- this exclude
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<includes>
</includes>
<excludes>
<exclude>**/*Dto.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
I know, that 1 option is to append the prefix to the generated class, f.i. _ and use this for filtering, but I am wondering if there is another option. How to specify the source project folder (src/main/java) and thus exclude all other folders? Is the plugin based only on package names?
I think that is not possible because your compiled classes are in the same directory in target. And Jacoco needs the compiled classes and therefore you can not make a filter on sources.
You can exclude classes in the Jacoco report by setting an exclude path but the values should be the path of compiled classes relative to the directory target/classes/.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</configuration>
</plugin>
The best solution would be to generate the classes in a specific package. But maybe you can't.
Simply doing the below solved my problem of ignoring a package rather than the files.
<configuration>
<excludes>
<exclude>**/basepkg/subpkg1/subpkg2/*</exclude>
</excludes>
</configuration>
This is how I excluded any class generated by Jooq
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-version}</version>
<configuration>
<excludes>
<exclude>**/jooq/**/*.class</exclude>
</excludes>
</configuration>
</plugin>
Currently, the project I am working on has all of the code for the web service in a single and is creating a war and a jar file.
The Jar file only contains the bare minimum of classes shared by other projects.
I don't have the option to split the shared classes out into a separate core project at this time. (Though I would prefer that)
I'm trying to put only the necessary java files in the source jar:
Common_Model-1.1.2.jar
Common_Model-1.1.2.jar-sources
I have something like this in my POM:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<includes>
<include>gov/app/api/common/models/**</include>
<include>gov/app/api/intra/models/**</include>
<include>gov/app/api/models/**</include>
</includes>
<finalName>Common_Model-1.1.2</finalName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<includes>
<include>gov/app/api/common/models/**</include>
<include>gov/app/api/intra/models/**</include>
<include>gov/app/api/models/**</include>
</includes>
<finalName>Common_Model-1.1.2</finalName>
</configuration>
</execution>
</executions>
</plugin>
While this does create the sources jar, it ignores the includes configuration and adds all of the .java files from the entire project.
I've also tried using <excludes>, but that too is ignored.
I tried this in my project:
<include>com/sandbox/**/Permutations*</include>
And the resulting Common_Model-1.1.2-sources.jar only had this one file in it named Permutations.java When I tried
<include>com/sandbox/Permutations*</include>
<include>com/sandbox/Sandbox*</include>
The jar had only those two files. So what you've got should work. I suspect your patterns are too broad.