maven-compiler-plugin how to change the classes destination directory - maven

By default the maven compiler plugin put the compiled classes into ${project.build.directory}/classes. I want to put them into ${project.build.directory}/myclasses. The argument -d changes the destination of the compiled classes. I configured the plugin but I got an error: javac: directory not found: C:\home\target/myclasses.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<showDeprecation>true</showDeprecation>
<compilerArguments>
<d>${project.build.directory}/myclasses</d>
</compilerArguments>
</configuration>
</plugin>

You should be able to do it like this:
<build>
<outputDirectory>${project.build.directory}/myclasses</outputDirectory>
</build>

The destination folder must exists. You can create it using a ant task:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>createClassesDir</id>
<phase>process-resources</phase>
<configuration>
<tasks>
<mkdir dir="${project.build.directory}/myclasses" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Related

Maven-replacer-plugin is not invoked with maven-war-plugin

I am trying to replace string %APP_NAME% with enviroment variable in jdbc.properties using maven. I have following configuration:
<pluginManagement>
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<basedir>${project.build.directory}/classes</basedir>
<includes>
<include>jdbc.properties</include>
</includes>
<replacements>
<replacement>
<token>%APP_NAME%</token>
<value>${env.BRANCH_NAME}</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
<executions>
<execution>
<!-- First step is to disable the default-war build step. -->
<id>default-war</id>
<phase>none</phase>
</execution>
<execution>
<!-- Second step is to create an exploded war. Done in prepare-package -->
<id>war-exploded</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
<execution>
<!-- Last step is to make sure that the war is built in the package
phase -->
<id>custom-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- <compilerArgument>-Xlint:all</compilerArgument> -->
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<compilerArguments>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</pluginManagement>
When i invoke:
mvn clean package
or
mvn clean install
the replacer plugin is not called. Can anyone can please explain why and what can I do to let it work? Or if replacer is not compatible with future war plugin can anyone explain me any other way to replace some string in jdbc.properties before building war? I saw also ant plugin but with same config it is not called too.. Example below..
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>deploy-ui</id>
<phase>package</phase>
<inherited>false</inherited>
<configuration>
<tasks>
<replace dir="${basedir}/src/main/resources">
<include name="**/jdbc.properties" />
<replacefilter token="%APP_NAME%" value="${env.BRANCH_NAME}"/>
</replace>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
The plugin is defined in a <pluginManagement> block:
<pluginManagement>
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
...
Find the <build><plugins> block for the POM where replacer needs to run, and add the following:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
</plugin>
Plugin Management is rather like a template for what should happen when the plugin is invoked. If the plugin is not referenced in the <build><plugins> block, nothing will happen.

How to remove a a specific directory from the Maven's target directory at the end of the build?

I have a task to unpack all the jars mentioned in the pom.xml and then jar the unpacked content into one single jar. I am able to do this using the unpack-dependency goal of the dependency plugin and the jar plugin.
However, after i generate the new jar, I want to delete the folder that was created after unpacking. I am using the following code snippet in my pom.xml(Please read the comments above each plugin).
<build>
<plugins>
<!-- This part of the code is used to unpack all the dependencies mentioned in my pom.xml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includes>**/*.class</includes>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<!-- This part of the code is used for creating a jar with all the contents of the "alternateLocation" directory above -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.directory}/alternateLocation</classesDirectory>
<outputDirectory>${project.build.directory}</outputDirectory>
<finalName>abc</finalName>
</configuration>
</execution>
</executions>
</plugin>
<!-- I am using this part of the code to delete the "alternateLocation" after everything is done, but it deletes the target directory instead -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${project.build.directory}/alternateLocation</directory>
</fileset>
</filesets>
<excludeDefaultDirectories>${project.build.directory}</excludeDefaultDirectories>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The clean plugin is basically deleting the target directory by default.
So how can I delete the "alternateLocation" folder from the target directory at the end of the build. (I guess you can do it using the maven-antrun-plugin but i don't want to use this plugin.).
You can solved this by using the maven-assembly-plugin via the predefined descriptor jar-with-dependencies which can be done by the following:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</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>
[...]
</project>
Than you don't need supplemental configuration.

Maven rename a file after everything else finishes

I have a project which I need to rename the final output file generated by the Maven Assembly Plugin after everything else finishes (in the compiling/building/assembly process).
The Maven Assembly Plugin is generating a final .zip file, based on the project's name, and I need to rename this completely to final-version.oxt. I'm trying to use the maven-antrun-plugin to rename it, as pointed by other similar questions here, but no luck (I've never used Maven or Ant before, so maybe I'm missing something).
This is the <build> section of the project's pom.xml. The rename part seems to be completely ignored, since no file is generated in my home folder.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
<descriptors>
<descriptor>src/main/assembly/ooo-jar.xml</descriptor>
<descriptor>src/main/assembly/ooo.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<configuration>
<tasks>
<copy file="${project.build.directory}/target/libreofficeplugin-ooo.zip"
tofile="/home/brunofinger/final-version.oxt" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
A few modifications made it work, probably the phase was wrong, but using <phase>install</phase> seems to make it work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/libreofficeplugin-ooo.zip" tofile="${project.build.directory}/final-version.oxt" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
This question is nearly an year old, but in case anyone else is facing similar issue, here is an alternate solution.
If you are looking for a more mavenish way of doing it,you can use
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>rename-file</id>
<phase>install</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.outputDirectory}/libreofficeplugin-ooo.zip</sourceFile>
<destinationFile>${project.build.outputDirectory}/final-version.oxt</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
and in case you want to copy instead of rename, use the copy goal instead of the rename goal.
You don't need to use antrun to rename the output file. Just use the tag finalName of the assembly plugin to rename the output file.
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
<descriptors>
<descriptor>src/main/assembly/ooo-jar.xml</descriptor>
<descriptor>src/main/assembly/ooo.xml</descriptor>
</descriptors>
<finalName>final-version.oxt</finalName>
</configuration>
</execution>
</executions>
</plugin>

Multiple MAVEN modules sharing a common source folder

A central POM defines multiple JAR modules what must package parts of a central, common source folder. This is a legacy project and I want to keep the source files in one single place. Is it possible to set up MAVEN to compile and package only parts of this source folder?
SK
You are right, this is the solution:
...
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${version.build.helper.maven.plugin}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>sk/maven/api/**/*.*</source>
<source>sk/ext/lib/**/*.*</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.maven.compile.plugin}</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<includes>
<include>sk/maven/api/**/*.*</include>
<include>sk/ext/lib/**/*.*</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<includes>
<include>sk/maven/api/**/*.*</include>
</includes>
</configuration>
</plugin>

exclude Test.java class from maven build war

I've tried to exclude some .java file of src/test/java from being packaged in maven build. My pom.xml is:
plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<configuration>
< test Excludes >
<exclude>**/Test1.java</exclude>
<exclude>**/Test2.java</exclude>
<exclude>**/Test3.java</exclude>
<exclude>**/Test4.java</exclude>
<exclude>**/Test5.java</exclude>
</testExcludes>
</configuration>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
enter code here
But When I'm trying to run my Test classes that is excluded from build, is giving ClassNotFoundException.
Kindly, guide me..
I have added below plugins so now its skipping the test cases:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

Resources