Maven: Exclude Java File from Packaging - java

I have a Java file which is executed during maven:compile phase to perform some file adjustments on each compilation.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>build-dump</id>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.domain.internal.UpdateFile</mainClass>
</configuration>
</plugin>
The thing is that this file is only for code maintenance and it is not needed in the final Jar package, so how can I exclude this file from being packaged into the final Jar while I keep it executing the maintenance code?
Thank you in advance.

Related

Compile generated maven project in generated-sources directory

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>

How to add src/test/java contents to the output maven jar that gets created

I see a lot of similar questions. But unable to make this work.
I have tried testresources and build-helper-maven-plugin so far
Also I read in 1 thread how to write my own assembly plugin to do something like that.
But posting this again to see if there are cleaner ways that I don't know of
This is existing code and i got to fix it. The thing is when i open the jar after a successful build i am unable to find the src/test/java classes inside the jar. We got a maven build-helper-maven-plugin and maven-jar-plugin. But I don't see the test classes in it still.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>test</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I see the generated classes in a test-classes directory inside the target folder. But not inside the jar
I want them inside the jar as I am depending on that jar in another project. The other project is not compiling because its importing that test class inside src/test/Java
I cannot create a new project just for this case as I don't have that liberty.
Did you try maven-dependency-plugin (instead of build-helper-maven-plugin) in combination with maven-jar-plugin ?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version><!--$NO-MVN-MAN-VER$-->
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>you-project-group-id</groupId>
<artifactId>you-project-artifact-id</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

using antlr4-maven-plugin for both main and test code

I'm working on a project in which I've got an antlr4 grammar in main code, and I'd like to add a "mini-grammar" for some tests. I'd like the generated .java files for that mini-grammar to only be available to test code. Can the antlr4-maven-plugin support this?
After some experimentation, I settled on this less-than-ideal setup:
both my main-targeted and test-targeted grammars are in src/main/resources (I realize this isn't the standard place; I set sourceDirectory to account for this)
the antrun plugin copies ${project.build.directory}/generated-sources/antlr4/**/MyTestGrammar*.java to ${project.build.directory}/generated-test-resources/antlr4
the build-helper-maven-plugin plugin adds ${project.build.directory}/generated-test-resources/antlr4 as a test source dir
This requires three plugin configurations, and that I explicitly specify which of the generated grammars are meant for tests and which are meant for main code. Is there a better way?
Save your test grammar in a subfolder of ${baseDir}/src/test/antlr4.
Then you can try and put something like this inside the build-plugins element of your POM:
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr4.plugin.version}</version>
<configuration>
<arguments>
<argument>-visitor</argument>
</arguments>
</configuration>
<executions>
<execution>
<id>antlr-4</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
<execution>
<id>antlr-test</id>
<configuration>
<sourceDirectory>${baseDir}/src/test/antlr4</sourceDirectory>
<outputDirectory>${baseDir}/target/generated-test-sources-antlr/antlr4</outputDirectory>
</configuration>
<phase>generate-test-sources</phase>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
and then add the generate sources while compiling the test classes:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${baseDir}/target/generated-test-sources-antlr/antlr4</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
You may want to adjust directories and packages names according to your needs

Excluding BuildConfig.class and R.class from Android library jar in Maven

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.

Place all libraries and files for a project in one folder

I use maven in my java build process. The following is a snippet of code that creates an single jar with all dependencies. In order to reduce the data transfer on small changes to the build I'd like to place all project files (including dependencies) in the folder target/build . I plan to rsync the folder with the remote machine running the app and run the app with:
java -cp target/build/* <classname>
How do I modify this snippet to achieve this? I've read the documentation here but don't know how to piece the fix together:
http://maven.apache.org/plugins/maven-assembly-plugin/
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Are you asking how to get maven to copy your dependencies to the target folder when you build?
I think you want the maven dependency plugin. It copies the dependencies of your project to an output folder you specify.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${targetDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
It sounds like you may also need to maven jar plugin to tell it where to package your jar to.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>${dir}</outputDirectory>
</configuration>
</plugin>
Use the maven dependency plugin
It has the gole: copy-dependencies. This should do what you want.
Example (take from the documentation)
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>

Categories

Resources