I have a maven project that compiles two different projects and then creates classes in this dir: ${project.build.directory}/classes
Where ${project.build.directory} points to the dir that pom.xml exists.
I’m using maven-jar-plugin with different “execution” blocks to make the jar files out of related directories/classes for each project. I’m very new to maven and have difficulty to define the right “include” and “exclude” directories.
This is the structure that my classes reside:
\target\classes\com
\target\classes\com\microsoft
\target\classes\com\google
\target\classes\org
The first jar file needs to be created out of these classes:
\target\classes\com\microsoft
\target\classes\org
And the second jar needs to be created out of these classes:
\target\classes\com\google
Following is the part of “build” block that has “execution” blocks to create these jars. The first jar is called: msn-prod and the other is called: google. As you see, I’ve tried all different combinations to create these jars and none worked - they exist in the following build block as the parts which are commented.
Can somebody please help me on this? Any help is greatly appreciated.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>msn-prod</id>
<goals><goal>jar</goal></goals>
<phase>generate-resources</phase>
<configuration>
<classifier>msn-prod</classifier>
<!-- <classesDirectory>${project.build.directory}/classes/com/microsoft</classesDirectory>
<includes>
<include>**/*.class</include>
</includes>
<classesDirectory>${project.build.directory}/classes/org</classesDirectory>
<includes>
<include>**/*.class</include>
</includes>-->
<classesDirectory>${project.build.directory}/classes</classesDirectory>
<!-- <includes>
<include>**/*.class</include>
</includes>-->
<!-- <excludes>
<exclude>**/com/google/*</exclude>
</excludes>-->
<!-- <excludes>
<exclude>**/google/*.class</exclude>
</excludes>-->
<includes>
<include>**/com/microsoft/*.class</include>
<include>**/org/*.class</include>
</includes>
<finalName>${msn.prod}-${msn.api.version}</finalName>
</configuration>
</execution>
<execution>
<id>google</id>
<goals><goal>jar</goal></goals>
<phase>generate-resources</phase>
<configuration>
<classifier>google</classifier>
<!-- <classesDirectory>${project.build.directory}/classes</classesDirectory>
<includes>
<include>**/com/google/*.class</include>
</includes>-->
<classesDirectory>${project.build.directory}/classes/com/google</classesDirectory>
<includes>
<include>**/*.class</include>
</includes>
<finalName>${google}-${google.api.version}</finalName>
</configuration>
</execution>
</executions>
</plugin>
You are violating the Maven best practice of one build artifact per module and therefore running into trouble. Just break it up into multiple projects and it will be easy.
Related
We have a set of integration tests, which all end with IT. Out of those, there is some specific subset, which we would like to execute separately. Let's say their names end with SpecialIT. So what we want to achieve is two configurations of the failsafe plugin:
To execute all tests, ending with IT, but not with SpecialIT -> this is easy. Just normal inclusion and exclusion by those names.
To execute all tests, ending with SpecialIT, but not all others ...IT.
I thought it would be natural to create some dedicated profile and use a separate failsafe configuration with a negative lookbehind regex for that, so ended up with this configuration (had to use < instead of <, as that one is not allowed there):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<excludes>
<exclude>%regex[.*(?<!Special)IT.*]</exclude>
</excludes>
<includes>
<include>**/*SpecialIT.*</include>
</includes>
</configuration>
</plugin>
But when I try to run this - I'm getting the following error:
Exclamation mark not expected in 'exclusion': %regex[.*(?<!Special)IT.*]
Reading the documentation of the failsafe plugin - I see this:
The syntax in parameter excludes and excludesFile should not use (!).
So the question is: is there any other way to achieve this, without, let's say, renaming all our integration tests into ...StandardIT and ...SpecialIT?
I was thinking in the direction of tags, test suit names or smth., but in our project we currently have a mix of JUnit5, JUnit4 and Spock (Groovy) tests, so it becomes not so straightforward.
P.S. If I just use this configuration - all IT tests are getting disabled and nothing is executed:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*IT.*</exclude>
</excludes>
<includes>
<include>**/*SpecialIT.*</include>
</includes>
</configuration>
</plugin>
Thanks to #andrey-b-panfilov, I realized that my problem was that I somehow was under an impression (based on this) that all ...IT tests are always executed by default... But this is, of course, only until the <includes> is overwritten. So, the solution is eventually to just define the following two configurations:
To execute all but SpecialIT:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*IT.*</include>
</includes>
<excludes>
<exclude>**/*SpecialIT.*</exclude>
</excludes>
</configuration>
</plugin>
To execute only SpecialIT:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*SpecialIT.*</include>
</includes>
</configuration>
</plugin>
I want to minify JS and CSS files in maven project. I have used minify-maven-plugin (com.samaxes.maven v1.7.6). As per the documentation (https://samaxes.github.io/minify-maven-plugin/minify-mojo.html), I have set <nosuffix> and <skipMerge> as true because I want to maintain the file structure and replace the minified files with original files. I have also set the <phase>package</phase>.
After generating and deploying the WAR file, the JS and CSS files are not minified, they stay the same as before.
I also referred to some stackoverflow answers and set the <warSourceExcludes> option as per the suggestion provided at https://stackoverflow.com/questions/22117824/using-samaxes-minify-nosuffix-to-overwrite-original-files
After using the <warSourceExcludes> option, when I deploy the WAR file on the server, the JS and CSS files are not available and the application is showing 404 errors for the same. Please refer to my pom.xml configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
</configuration>
</plugin>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.6</version>
<executions>
<execution>
<id>default-minify</id>
<phase>package</phase>
<configuration>
<charset>UTF-8</charset>
<webappSourceDir>${project.basedir}/WebContent</webappSourceDir>
<cssSourceDir>./</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssSourceExcludes>
<cssSourceExclude>**/*.min.css</cssSourceExclude>
</cssSourceExcludes>
<jsSourceDir>./</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceExcludes>
<jsSourceExclude>**/*.min.js</jsSourceExclude>
</jsSourceExcludes>
<jsEngine>CLOSURE</jsEngine>
<nosuffix>true</nosuffix>
<skipMerge>true</skipMerge>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
Please suggest a proper solution. Thanks in advance !
The answer given by #Kristof Neirynck for the question How to get maven to build a war with minified files using yuicompressor-maven-plugin works for the issue mentioned.
I changed the <phase> property as prepare-package and added
<webappTargetDir>${project.build.directory}/minify</webappTargetDir>
in minify-maven-plugin after <webappSourceDir> and in the maven-war-plugin I set the following configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<webResources>
<resource>
<directory>${project.build.directory}/minify</directory>
</resource>
</webResources>
</configuration>
</plugin>
After using this configuration, when I deploy the WAR file in the Tomcat the original files are replaced with the minified files.
I have an antrun task (goal run on phase prepare-package) set up that creates a file and saves it in /target/foo.bar. How do I add that to the artifact that gets created by maven (depending on module, it could be a jar or a war file)?
I have tried it with resources, with the builder-helper plugin, and the jar plugin - no luck:
<resources>
<resource>
<directory>target</directory>
<includes>
<include>**/foo.bar</include>
</includes>
</resource>
</resources>
That doesn't seem to do anything.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/foo.bar</file>
<type>bar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
According to debug output, this seems to install something extra in the repo, but doesn't add foo.bar to the artefact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<includes>
<include>../foo.bar</include>
</includes>
</configuration>
</plugin>
No observable effect, either. :(
(I assume this might work for the war file, but I'd prefer one way to do it for both artifact types, if possible - plus I really need this to work for the jars, too...)
Is there a way to do what I want to do?
(I'd prefer not saving my file in src/main/resources first; I thought the packaging processes would pick files up from the build directory after they were placed there anyway, but I suppose I misunderstood something...)
I had my file saved to project.build.directory. Changing that to project.build.outputDirectory means the jar plugin is picking it up, without the need of any other plugins. Unfortunately, that doesn't sort the war issue out... :(
I had a similar problem and stored the resulting artifact in
target/${project.name}-${project.version}/foo.war
To be more precise, I created the war from a directory using the ant target
<war warfile="target/${project.name}-${project.version}/foo.war" basedir="somedir" />
Then it was correctly added to the surrounding ear.
Maven 2.2.1
JDK - 1.6.0_45
[WARNING] JAR will be empty - no content was marked for inclusion!
BUILD SUCCESSFUL
But build creates jar with pom.xml but no class files.
On the maven source code this exception is thrown only when source directory is not found.
The build is working for all other developers except on my workstation and one more workstation
I have tried all the solutions provided for this issue on stack overflow.
My source directory is src/java.
I also created src/main/java as source still no result.
I am calling mvn -o jar:jar && call mvn -o antrun:run
-o is becuase at this point I am testing with some old jars.
<build>
<sourceDirectory>src/java</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>${basedir}/src/test/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<debug>true</debug>
<optimize>false</optimize>
<showDeprecation>true</showDeprecation>
<source>1.5</source>
<target>1.5 </target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>test*/temp/*.java</exclude>
<exclude>test*/support/*.java</exclude>
<exclude>test*/debug/*.java</exclude>
</excludes>
<includes>
<include>test*/**/AllTests.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>default-cli</id>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${artifactId}-${version}.jar"
todir="${user.home}/.m2/repository/${groupId}/${artifactId}/${version}" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
First follow the conventions in Maven which means your production sources code should be in src/main/java. You should also locate your resources like property files or other kind of files (like xml files) in your case to the proper location which is for production src/main/resources and for unit tests src/test/resources.
The first thing you should change is the directory structure for your project in the process in migration. That will save many hassles with configurations in Maven cause you are violating the convention over configuration paradigm.
Your unit tests code in src/test/java and follow the naming conventions for unit tests which means name your unit tests like *Test.java nothing more. You don't need to define a suite to run all the tests. If you follow the naming convention maven-surefire-plugin will do all the work for you.
Remove the antrun plugin from your pom configuration and use
mvn install
instead to install your produced jar into local repository. Based on the build life cycle you will compile, unit test and package your code into resulting jar files.
Usually in Maven there is no reason to call mvn jar:jar separately.
Apart from that all you should stop using Maven 2.2.1 cause it has defined End Of Life. Better start with Maven 3.X instead. But everything i wrote before is valid Maven 3.
I got Build Success but same error:
JAR will be empty - no content was marked for inclusion.
It was a test project and I realized that I had no "main" under "src". As soon as I corrected this, it was fixed. I am adding the wrong and right structure screenshots in the attachments:
right structure
wrong structure - missing main folder
Because of various issues with the XSDs I need to compile (described in other SO posts), I have a bindings file and also a local extension schema. The following command line works correctly, but I'm having trouble figuring out the right pom.xml configuration to mimic this:
xjc -nv src/main/resources/TCIP_4_0_0_Final.xsd src/main/resources/local/ObaCcLocationReport.xsd -b src/main/resources/local/rename.xjb -d target
Mainly, how do I specify more than one XSD? I tried:
<schemaDirectory>src/main/resources</schemaDirectory>
<schemaIncludes>
<include>TCIP_4_0_0_Final.xsd</include>
<include>local/ObaCcLocationReport.xsd</include>
</schemaIncludes>
but it seemed to ignore the second include.
I also tried variations on:
<schema>
<fileset>
<directory>src/main/resources</directory>
<includes>
<include>TCIP_4_0_0_Final.xsd</include>
<include>local/ObaCcLocationReport.xsd</include>
</includes>
</fileset>
</schema>
without success. Suggestions?
EDIT
This works as a workaround, but it's not ideal:
Since ObaCcLocationReport.xsd depends on schemas that get compiled as part of TCIP_4_0_0_Final.xsd, I just had to make sure it got compiled after that, and it seems to process files in filepath order. So I put the ObaCcLocationReport.xsd into an x subfolder and changed the pom.xml to:
<schemaDirectory>src/main/resources</schemaDirectory>
<schemaIncludes>
<include>TCIP_4_0_0_Final.xsd</include>
<include>x/ObaCcLocationReport.xsd</include>
</schemaIncludes>
This compiled the schemas and generated the Java files correctly.
Disclaimer: I am the author of the maven-jaxb2-plugin.
So your compilation depends on the order of the schema files in the XJC command? Hm, interesting. Why?
Please post mvn -X clean generate-sources log in such cases.
It really seems that Maven does not maintain the order of the file patterns as I also get:
schemas=[file:/.../src/main/resources/local/ObaCcLocationReport.xsd,
file:/.../src/main/resources/TCIP_4_0_0_Final.xsd]
Which is not what you want. Can be fixed, please file an issue. (I rely on one of the Maven libraries here, but can do it differently to maintain the order.)
You can configure it as follows:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<strict>false</strict>
<schemaIncludes/>
<schemas>
<schema>
<fileset>
<includes>
<include>TCIP_4_0_0_Final.xsd</include>
</includes>
</fileset>
</schema>
<schema>
<fileset>
<includes>
<include>local/ObaCcLocationReport.xsd</include>
</includes>
</fileset>
</schema>
</schemas>
</configuration>
</execution>
</executions>
</plugin>
Gives you:
schemas=[file:/.../src/main/resources/TCIP_4_0_0_Final.xsd,
file:/.../src/main/resources/local/ObaCcLocationReport.xsd]
Notes:
Don't forget <schemaIncludes/> otherwise src/main/resources/*.xsd will be included by default.
<strict>false</strict> gives you -nv.
You can always do args/arg to configure xjc on the low level, but I won't recommend it.