I'm trying to set up Maven on a node project I'm working on for build reasons. I've added the following minimal pom.xml file to the project:
<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>project.id</groupId>
<artifactId>project-title</artifactId>
<version>0.0.1</version>
<name>project-name</name>
<description>Project Description</description>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<!--<executable>npm start</executable> -->
<executable>echo 'HELLO'</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
However, now when I run mvn install it starts, begins to download the plugin but never finishes. Here is the command and console:
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project-name 0.0.1
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.2.1/exec-maven-plugin-1.2.1.pom
Now when I curl or visit the address, I can see the xml file perfect. Can anyone give me any direction on how to head from here? I'm using Maven 3.1.1 and Java 7.
Check your settings.xml file, which Maven looks for in the following locations and order:
$HOME/.m2/settings.xml
$M2_HOME/conf/settings.xml
There is a section in the settings.xml file for proxies and mirrors, which may be why you're seeing inconsistent bhavior between curl and mvn
EDIT
See Maven docs on settings.xml for more details:
<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>
In some cases it takes long time for maven to understand network problems. I saw situations when it "hangs" for 15 minutes on secured network without proper config for proxy.
I didn't use curl, but I wonder if it can use system proxy settings? Maven should be configured separately, he doesn't understand them.
Related
I am trying to build a nar file from maven repo central. I am not very used to maven, so I will explain the steps I followed until the blocking point where I am stuck now.
I want to generate the nar files for this artifact:
https://mvnrepository.com/artifact/org.apache.nifi/nifi-hwx-schema-registry-nar/1.10.0
So I created this pom.xml file:
<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>TestMaven</groupId>
<artifactId>TestMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-hwx-schema-registry-nar</artifactId>
<version>1.10.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<artifactItems>
<artifactItem>
<!-- hardcode values, or use properties, depending on what you want
to do -->
<groupId>TestMaven</groupId>
<artifactId>TestMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>[ packaging ]</type>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And then I try to compile whit this commands (first commands are to include maven dependencies on the PATH):
export JAVA_HOME=/usr/jdk64/jdk1.8.0_112
export M2_HOME=/usr/local/apache-maven
export M2=$M2_HOME/bin export PATH=$M2:$PATH
mvn -U -X dependency:copy-dependencies -DskipTests
-Dclassifier=sources -DoutputDirectory=target -Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=80 -Dhttps.proxyHost=X.X.X.X -Dhttps.proxyPort=80
And I am getting this error, which says that the maven dependency couldn't be found:
[ERROR] Failed to execute goal on project TestMaven: Could not resolve
dependencies for project TestMaven:TestMaven:jar:0.0.1-SNAPSHOT: Could
not find artifact
org.apache.nifi:nifi-hwx-schema-registry-nar:jar:1.10.0 in central
(https://repo.maven.apache.org/maven2) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to
execute goal on project TestMaven: Could not resolve dependencies for
project TestMaven:TestMaven:jar:0.0.1-SNAPSHOT: Could not find
artifact org.apache.nifi:nifi-hwx-schema-registry-nar:jar:1.10.0 in
central (https://repo.maven.apache.org/maven2)
Thank you
You would need to get the source code for Apache NiFi version 1.10.0 and then build that module.
You could get the code by cloning the git repo and checking out the tag rel/1.10.0.
https://github.com/apache/nifi/tree/rel/nifi-1.10.0/nifi-nar-bundles/nifi-standard-services/nifi-hwx-schema-registry-bundle
Then run mvn clean package from the location above.
How do I externally define the version tag in pom.xml for a jar project, with no parent pom?
I've used properties-maven-plugin and flatten-maven-plugin to get real close. The resulting pom.xml that gets deployed has an unusable version because of the value substitutions being used by properties-maven-plugin to set version. flatten-maven-plugin resolves issues with dependency versions, but does not seem to resolve the version of the primary artifact. The code does appear to build and name the jar correctly.
I looked into https://maven.apache.org/maven-ci-friendly.html and learned about ${revision}, but the examples there seem to include parent poms.
I'm hoping someone has come across a solution without use of a parent pom. If there's no solution, I'd like to consider this a feature request for one of the plugins, but I'd like to try here first before I submit such a request.
This is the simplest configuration I can provide to demonstrate my issue.
pom.xml
<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>testgroup</groupId>
<artifactId>test</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lang>3.9</lang>
<revision>${major}.${minor}</revision>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${lang}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>pre</id>
<phase>pre-clean</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>test.properties</file>
</files>
</configuration>
</execution>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>test.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<flattenMode>defaults</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
test.properties
major=1
minor=2
src/main/java/testgroup/test/App.java
package testgroup.test;
import org.apache.commons.lang3.StringUtils;
public class App
{
public static void main( String[] args )
{
System.out.println(StringUtils.chop("boo"));
}
}
Resulting .flattened-pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>testgroup</groupId>
<artifactId>test</artifactId>
<version>${major}.${minor}</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
I expect the version in .flattened-pom.xml to be 1.2, not ${major}.${minor}
So, I'm not sure if this is something I'm doing wrong, or some kind of bug in one of the plugins. For example, even Maven displays this weirdly:
C:\Users\Robert\eclipse-workspace\test>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< testgroup:test >---------------------------
[INFO] Building test ${major}.${minor}
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (pre) # test ---
< snip >
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) # test ---
[INFO] Building jar: C:\Users\Robert\eclipse-workspace\test\target\test-1.2.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
I don't know if you managed to find a way to resolve this, but from what I see, you are correctly setting the $revision property to the version element.
But do you run the tests? In order to build/test the application you need to run it like this:
mvn clean flatten:flatten test
or
mvn clean flatten:flatten package
What I find a bit off is that you are using $major and $minor without having them initialized in your pom.xml as properties and not using properties instead.
I understand that you would like to use the properties file which I assume you inject it somehow in your test environment. Instead you could use properties <major> and <minor> to initialize them and when you want to change them you can use
mvn clean -Dmajor=1 -Dminor=3 flatten:flatten test
If you have found some other way to resolve this, please share it with the rest of us :)
upgrade maven >= 3.5.0
change goal <phase>process-resources</phase> to <phase>package</phase>
it will solve the issue.
see also:
Maven CI Friendly Versions
The following is my pom.xml
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.2.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
<configuration>
<propertiesJMeter>
</propertiesJMeter>
</configuration>
</plugin>
</plugins>
</build>
When I run the .jmx, I get the following message:
Error: Could not find or load main class org.apache.jmeter.NewDriver
I notice that the classpath for org.apache.jmeter.NewDriver is wrong. How do I set it to Jmeter's home in the pom.xml, or in the .jmx file?
There is no such concept as JMeter home when it comes to executing tests via Maven plugin, all you need to do is to:
Set up your pom.xml file to look like:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.blazemeter</groupId>
<artifactId>mvn-jmeter</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>maven-jmeter-demo</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.2.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Set up your project to look like:
src
test
jmeter
test.jmx
here you can put another jmx if needed
pom.xml
Run your test like mvn clean verify
JMeter Maven plugin will download JMeter along with dependencies (you will be able to find in under target/jmeter folder along with JMeter logs (logs folder) and test results (results folder)
More information:
JMeter Maven Plugin - official documentation
JMeter Maven Plugin - JMeter Wiki
Five Ways To Launch a JMeter Test without Using the JMeter GUI - aggregate information on different approaches to running a headless JMeter inlcuding (but not limited to) Maven Plugin
You CAN use Jmeter home with the Maven plugin. You just have to take one more step after editing your pom as described above.
In your projects directory, open up your cmd and run a specific execution to the Jmeter goal; e.g. : mvn com.lazerycode.jmeter:jmeter-maven-plugin:2.7.0:jmeter
Equivalent statement : mvn groupId:artifactId:version:goal (based on POM structure of plugin)
This will generate the Jmeter directory inside of your target directory. You can then use it as the Jmeter home for the code you're trying to use.
When I do a mvn clean package locally everything works fine. When I do a mvn clean package on the jenkins server I get errors. I am running Jenkins 1.596, Maven 3.2.5, SonarQube 4.5.1, Maven plug-in 2.4 is defined in the Jenkins sonar settings.
This is the output from the Jenkins job.
[INFO] --- sonar-maven-plugin:2.4:sonar (default-cli) # myproject ---
[INFO] SonarQube version: 4.5.1
INFO: Work directory: /var/lib/jenkins/workspace/myporject/target/sonar
[INFO] [22:48:12.147] Base dir: /var/lib/jenkins/workspace/myproject
[INFO] [22:48:12.148] Working dir: /var/lib/jenkins/workspace/myproject/target/sonar
[INFO] [22:48:12.148] Source paths: src
[INFO] [22:48:12.148] Test paths: src/test/java
[INFO] [22:48:12.148] Binary dirs: target/classes
[INFO] [22:48:12.148] Source encoding: UTF-8, default locale: en_US
[INFO] [22:48:12.148] Index files
[ERROR] File [relative=src/test/java/com/blah/web/service.java, abs=/var/lib/jenkins/workspace/myproject/src/test/java/com/blah/web/service.java] can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files
[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.4:sonar (default-cli) on project myproject: File [relative=src/test/java/com/blah/web/service.java, abs=/var/lib/jenkins/workspace/myproject/src/test/java/com/blah/web/service.java] can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.4:sonar (default-cli) on project myproject: File [relative=src/test/java/com/blah/web/service.java, abs=/var/lib/jenkins/workspace/myproject/src/test/java/com/blah/web/service.java] can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files
It appears that something is missing from my Sonar plug-in settings but I'm not sure what or where to define it.
Here are some snippets from my pom.xml
<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>com.blah</groupId>
<artifactId>myproject</artifactId>
<version>99</version>
<packaging>war</packaging>
<name>myproject</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<routePath>${pom.project.build.directory}/routes</routePath>
<spring.release.version>3.2.4.RELEASE</spring.release.version>
<rabbitmq.version>3.1.4</rabbitmq.version>
<spring.amqp.version>1.2.0.RELEASE</spring.amqp.version>
</properties>
<build>
<finalName>myproject</finalName>
<sourceDirectory>src</sourceDirectory>
<scriptSourceDirectory>src/main/java</scriptSourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.1.8-01</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
</additionalProjectnatures>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
<classpathContainer>GROOVY_DSL_SUPPORT</classpathContainer>
</classpathContainers>
<sourceIncludes>
<sourceInclude>**/*.groovy</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<includes>
<include>**/integration/**/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The problem seems to be that your source folder is a prefix of your test folder. In other words, if you say that your test folder is src/test/java/, your source folder should be src/main/java, not src since this causes the plugin to find the same class twice, which is what causes the error
Removing <sourceDirectory>src</sourceDirectory> should do the trick since it will use maven's default (src/main/java). While you're at it, remove also testSourceDirectory since you are setting it to the default value, so you don't need it. In general the pom should be as simple as possible since maven is all about convention over configuration
Given a <plugin> element in a pom.xml, how do I find the default phase that it binds to?
For example, I'd like to know which phase of the Maven lifecycle does the maven-war-plugin gets executed.
The best way to see what's really happening in your project along those lines is with mvn help:effective-pom. It doesn't just show the defaults; it shows what actually is according to your current pom.
I'm having a problem with the above-reply.
Here's a simple pom. It uses an annotation processor plugin, which is bound to generate-sources by-default, since I didn't specify a <phase>.
<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>com.test</groupId>
<artifactId>test-simple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.0.5</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
mvn generate-resources does indeed invoke the plugin...
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-simple 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-processor-plugin:2.0.5:process (default) # test-simple ---
Yet scanning the output of mvn help:effective-pom doesn't yield any clue to the default binding of this plugin.
$ mvn help:effective-pom |grep generate-sources; echo $?
1
The only way I've so-far found to list default phase binding is by examining plugin source.