Maven Update changes .classpath failing build while war packaging - maven

I've a Spring Maven webapp project which is to be deployed in Tomcat.
I want to package my project to .war for which I've my POM.xml as follows.
<!-- Other settings -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>
<!-- other settings -->
With this setting, when I do Maven -> Update from m2eclipse and Run as -> Run on Server, it gives ClassNotFoundException for ContextLoaderListener.
But when I add Maven Dependencies to my java build path from Deployment Assembly from project properties as described in https://stackoverflow.com/a/15780350/1433665 , the project runs fine. But again when I do Maven Update, same problem persists.
How can I configure my pom.xml such that I don't have to edit Deployment Assembly settings everytime I do Maven -> Update ?
Also, only file that is changed after Maven -> Update is .classpath in which
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
is updated to
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>

Related

How to externalise .properties files when deploying a Spring Boot WAR in Wildfly Server?

I am developing a web application using Spring Boot, and want to generate war instead of jar.
It works fine using the conversion from jar to war described here : http://spring.io/guides/gs/convert-jar-to-war/
But I want to externalize the application.properties from the war, because I want to be able to modify it without opening the war archive.
I already defined the spring-boot-maven plugin.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
com.application.Application
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
I think I need to add Dependency: config to my manifest file.
So, I've done it like that :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Dependencies>config</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
But when I launch the Application.war on Wildfly 8.4, I've got this
{"JBAS014671: Failed services" => {"jboss.module.service.\"deployment.Application.war\".main" => "org.jboss.msc.service.StartException in service jboss.module.service.\"deployment.screening.war\".main: JBAS018759: Failed to load module: deployment.Application.war:main
Caused by: org.jboss.modules.ModuleNotFoundException: config:main"}}
I would like that my application start with my custom MANIFEST.MF (with Dependency: config) so that I can externalize my application.properties file.
Thank you.
The problem was on the server side !
When you specify a package, you need to add a module.xml file with a Wildfly server.
So in modules/config/main/, I've added my application.properties and a module.xml file that contains :
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="config">
<resources>
<resource-root path="."/>
</resources>
</module>
Thank you for your response #NielsNet.

IntelliJ IDEA not "hyperlinking" to source files in Maven Run tool window

I have been using intellij for 2 years now and love it. however after updating to 2016.3.4 it stopped highlighting path locations in the run window.
I used to be able to just click on the highlighted path and it would jump to the file and line.
example of output from maven-checkstyle-plugin which should be "clickable" :
[ERROR] C:\Users\userName\Documents\project.main\src\main\java\MyClass.java:36: Only one statement per line allowed. [OneStatementPerLine]
This is extremely frustrating and any idea of how to get intellij to do this again would be fantastic.
Minimal, Complete, and Verifiable example
To Replicate this Create a project in IntelliJ IDEA called "bug"
IntelliJ IDEA 2016.3.4
Build #IU-163.12024.16, built on January 31, 2017
Create a folder
bug->CodeStyle
and a file in this folder
"checkstyle.xml"
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="charset" value="UTF-8"/>
<property name="severity" value="error"/>
<property name="fileExtensions" value="java,"/>
<module name="FileTabCharacter"/>
<module name="TreeWalker">
<module name="RegexpSinglelineJava">
<property name="format" value="System\.(out|err).*?$"/>
<property name="ignoreComments" value="true"/>
</module>
</module>
</module>
the pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bugExample</groupId>
<artifactId>bug</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<configLocation>CodeStyle/checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>6.18</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
and a Class Example.java
public class Example {
public Example() {
System.out.println("This is not allowed");
}
}
And now in the maven project Tool Click life-cycle, and then Validate.
The outputs in the "run terminal" :
[INFO] Starting audit...
[ERROR] C:\Users\Username\bug\src\main\java\Example.java:7: Line matches the illegal pattern 'System\.(out|err).*?$'. [RegexpSinglelineJava]
Audit done.
So the problem is that Example.java is Not "Clickable", it has been in the past.
Thanks for the example, I've reported a bug for IntelliJ IDEA Maven integration, feel free to vote:
IDEA-169034 File paths are not clickable in the Maven tool output

How to fully disable javadoc checking with checkstyle maven plugin

i want to use the Maven Checkstyle plugin with a custom configuration that tells Checkstyle to not warn or error on missing Javadoc. Is there a way to do this?
Just found it myself. To fully ignore all javadoc checking for everthing, add this to your checkstyle configuration:
<!-- No need for Javadoc -->
<module name="JavadocType">
<property name="severity" value="ignore"/>
</module>
<module name="JavadocMethod">
<property name="severity" value="ignore"/>
</module>
<module name="JavadocVariable">
<property name="severity" value="ignore"/>
</module>
One good option would be configuring a suppressions filter.
Plugin configuration:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- ... -->
<build>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>verify</id>
<phase>verify</phase>
<configuration>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<suppressionsLocation>
checkstyle-suppressions.xml
</suppressionsLocation>
<suppressionsFileExpression>
checkstyle.suppressions.file
</suppressionsFileExpression>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- ... -->
</project>
checkstyle-suppressions.xml file:
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.0//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_0.dtd">
<suppressions>
<suppress checks="Javadoc" files="."/>
</suppressions>
Then running
$ mvn verify
Does not output any Javadoc-related Checkstyle errors.
Many other examples on suppressions filters may be found in checkstyle repository.
Below is the example with the Gradle 7.3.2 setup.
build.gradle
plugins {
id 'checkstyle'
}
Folder structure (root level).
├── config
   └── checkstyle
   ├── checkstyle.xml
   └── suppressions.xml
Below is the entry in checkstyle.xml to include suppressions.xml
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/suppressions.xml" />
</module>
And now you can have the suppressions in the suppressions.xml file to ignore a particular type of check.
for e.g. Below code base will ignore the mentioned checks for all files (regex can be applied to ignore particular matching set of files.)
<suppressions>
<suppress files="." checks="JavadocMethod"/>
<suppress files="." checks="JavadocPackage"/>
<suppress files="." checks="JavadocVariable"/>
<suppress files="." checks="MissingJavadocMethod"/>
<suppress files="." checks="JavadocPackage"/>
</suppressions>
Now execute the Checkstyle task on consolidated project files by
gradle check
If specific execution is needed then use checkstyleMain or checkStyleTest to execute checkstyle report on Source files or test files respectively.

Running TestNG tests in Maven fails

I am trying to run TestNG tests in Maven. here is my configuration:
pom.xml:
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
This is the testNG conf file:
<suite name="Suite1">
<test name="Test1">
<groups>
<run>
<include name="Setup" />
<include name="Material" />
</run>
</groups>
<packages>
<package name="coloright.hibernate.*" />
</packages>
</test>
when I run with eclipse - no problem.
when I run with mvn test - all test run successfully, but build failed with error:
suiteXmlFiles is configured, but there is no TestNG dependency
Please help
Looks like you are hitting this surefire bug, which contrary to the status looks to be still open.
The bug appears if surefire is unable to find the file specified in <suiteXmlFile>. Could you try just specifying testng.xml omitting src/test/resources to see if that helps? The documentation is silent on how this location is to be specified - whether it should be relative to the base directory or relative to test resources folder.
You could try this. Not sure if it would work for you but I do this sometimes, although it seems like its redundant:
<suiteXmlFiles>
<suiteXmlFile>
${project.build.testOutputDirectory}/testng.xml
</suiteXmlFile>
</suiteXmlFiles>
Add the surefire-testng dependency to your pom.xml :
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.16</version>
</dependency>
Please check your output folder in build path should be like /target/test-classes.
Also check your pom entry for this and update accordingly:
<testSourceDirectory>src</testSourceDirectory>
<outputDirectory>target/test-classes</outputDirectory>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>

Maven compile a source jar dependency

Let's say I have a project that uses a dependency that can be found in the Maven repository. However, lets also say that the jar file that will be downloaded is NOT in a format that is suitable for my project (e.g. if my maven project is an Android project and the jar has been compiled in a way the dex tool does not like). What I want to do instead is to downloaded the source version of that dependency. Once I add java-source, however, the classes are not accessible anymore from my own source code. I would like that maven downloads the source jar and compiles the java files inside it and places the compiled class files in the classpath. Is that possible?
My only alternative is to create a new project containing that library myself, but that's cumbersome.
You could do the following:
Use maven dependency plugin's unpack goal and place the contents of the dependency into a folder
Use build-helper-maven-plugin's add-source goal to add this folder as a source folder
Here is some code snippet...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>unpack</id>
<phase>process-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>my.group</groupId>
<artifactId>my.artifact</artifactId>
<version>my.artifact.version</version>
<classifier>sources</classifier>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/my.artifact</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/my.artifact.source</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Downloading the source packages using Maven is easy:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<classifier>sources</classifier>
</dependency>
How to configure Maven to expand this dependency and then compile it's contents is beyond me....
Have you considered an ANT solution? The ivy plug-in provides it with Maven-like abilities and the groovy plug-in can be used to script your special build logic:
build.xml
Ivy uses "configurations" (similar to Maven scopes) to group dependencies.
In this example the "sources" configuration holds the downloaded source packages. These are placed into a referenced fileset, which can be processed sequentially by the groovy task.
Each downloaded source jar is unzipped into the "build/src" directory:
<project name="demo" default="unzip-sources" xmlns:ivy="antlib:org.apache.ivy.ant">
<property name="build.dir" location="build"/>
<property name="src.dir" location="${build.dir}/src"/>
<target name="resolve">
<ivy:resolve/>
<ivy:cachepath pathid="build.path" conf="build"/>
<ivy:cachefileset setid="sourcezips" conf="sources"/>
</target>
<target name="unzip-sources" depends="resolve">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
project.references.sourcezips.each {
ant.unzip(src: it, dest: properties["src.dir"])
}
</groovy>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
</project>
ivy.xml
Each source package dependency uses the "sources" configuration. This maps directly to the "sources" scope of the Maven module.
<ivy-module version="2.0">
<info organisation="org.myspotontheweb" module="demo"/>
<configurations>
<conf name="build" description="ANT tasks"/>
<conf name="sources" description="Source packages"/>
</configurations>
<dependencies>
<!-- Build dependencies -->
<dependency org="org.codehaus.groovy" name="groovy-all" rev="1.8.2" conf="build->default"/>
<!-- Source dependencies -->
<dependency org="log4j" name="log4j" rev="1.2.16" conf="sources"/>
<dependency org="commons-lang" name="commons-lang" rev="2.6" conf="sources"/>
</dependencies>
</ivy-module>

Resources