I'm having a problem with Maven behind a squid proxy server.
I have a module of my system that depends external communication with a remote webservice.
I have my maven proxy configurations under ~/.m2/settings.xml, but apparently, these information are been used just for dependencies downloads.
When I run 'mvn test', these configurations aren't used on command line execution call. This is the instruction echoed on console:
${JAVA_HOME}/bin/java -jar /tmp/surefirebooter4156656684210940660.jar /tmp/surefire2646147996861949548tmp /tmp/surefire3498083351425809633tmp
There's a way to pass arguments to JVM during tests and other maven method executions?
Perhaps this can be of interest to you: How do I configure proxy settings for Java. Alternatively you can try these startup parameters:
-Dhttp.proxyHost=url
-Dhttp.proxyPort=port
-Dhttp.proxyUser=user
-Dhttp.proxyPassword=pass
[EDIT]
These properties can also be set in MAVEN_OPTS. This post describes how this would work for the test-profile.
To fix some arguments on pom.xml, we can configure -DforkMode=never directly on surefire plugin configuration. i.e.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<argLine>-DforkMode=never</argLine>
</configuration>
</plugin>
The solution based on -DforkMode was suggested by this post, referenced by #johan-sjoberg on comments posted here
Related
I need to set a active profiles to the jars while compile the maven spring boot project
Following are my two Approaches tried to activate the profiles.
First Approach - Not Activating the Profiles
mvn clean package -Dspring-boot.run.profiles=dev help:active-profiles
-s settings.xml
*
Above command not setting the active profiles while executing jars
java -jar package.jar
I have using maven plugin dependencies for activating the first approach
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions> </plugin> </plugins></build>
Second Approach - Working as expected.
mvn clean package -Dspring.profiles.active=dev help:active-profiles -s
settings.xml
java -jar -Dspring.profiles.active=dev package.jar
I'm using multi-module parent pom structure for the project. Any one pls advise to work the first approach?
Maven is a build system. Its responsible for building your artifact.
Maven's profiles are basically a tool that allows to slightly change the build process depending on various factors (operating systems, version of java and so forth).
These are defined in maven's files (pom.xml)
Spring (and Spring boot of course) as opposed to maven is a runtime framework.
Spring profiles is something totally different - they allow to load different beans, resolve different configurations in runtime (read after you call java -jar app.jar) depending on profile definitions.
So don't be confused with the same name, its only an "accidental coincidence"
Now as for your question.
Maven builds the artifact and packages it for that use spring boot maven plugin. If you want to customize this building process - use maven profiles (and as a consequence help:effective-profiles can be sometimes handy).
When the artifact is built - you can run it. For that you can use spring profiles to define runtime in-variants:
- Addresses of databases
- Credentials
- Some subsystems of your product that won't be run locally
and so forth, this list really depends on the application itself.
There is an option to run the spring boot application right from maven and for that you can really use -Dspring.profiles.active=dev but if you don't run it (and you don't in any of suggested approaches, read about mvn spring-boot:run to understand what does it really mean to run a spring boot application during the build), then:
First approach:
mvn clean package -Dspring-boot.run.profiles=dev help:active-profiles
-s settings.xml
-Dspring-boot.run.profiles is irrelevant here - you do nothing with it (again you don't run the project during the build) so it does nothing
java -jar package.jar
Here you can really specify spring profiles with --spring.profiles.active=dev,whatever
The Second approach:
mvn clean package -Dspring.profiles.active=dev help:active-profiles -s
settings.xml
Again, -Dspring.profiles.active=dev is irrelevant, it does nothing.
java -jar -Dspring.profiles.active=dev package.jar
Here you do specify the list of active profiles (just like --spring.profiles.active, from spring boot's standpoint its the same) That's why the application works in runtime as expected
What is the basic difference between maven compiler plugin and maven surefire plugin?
Also, I wanted to define system variables inside the pom.xml, under plugin configuration and read it from my java code(Using System.property()).
<configuration>
<systemPropertyVariables>
<envName>testEnv</envName>
</systemPropertyVariables>
</configuration>
This configuration is working with maven surefire plugin and I am able to read it in my java file, however, same is not working with maven compiler plugin.
Those are two different plugins.
maven-compiler-plugin, as its name suggests, handles compiling your code.
maven-surefire-plugin handles [unit] test execution and failing the build process if there are test failures.
I have a maven project, and I can run coverage on it on my own machine, no problem after following instructions here: https://confluence.atlassian.com/clover/clover-for-maven-2-and-3-quick-start-guide-160399608.html
When I use Jenkins to run clean clover:setup test clover:aggregate clover:clover, it complains that it Could not find goal 'setup' in plugin org.apache.maven.plugins:maven-clover-plugin:2.4 among available goals aggregate, check, instrumentInternal, instrument, log, clover, save-history
Obviously there is no settings file on the Jenkins box. I'm not sure how it even knows those available goals offhand. And I'm not sure how to add that last goal.
I tried this to no avail:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-jira-plugin</artifactId>
<version>5.0.18</version>
</plugin>
</plugins>
</pluginManagement>
The second plugin is what I added, in the hopes to mirror what the settings.xml file was doing on my local machine.
I figure another option is to edit the settings file on Jenkins itself. That is probably the best one...
My Jenkins is in Docker so I just ran docker exec -it abc /bin/bash and got in. When I go to .m2 I don't see a settings.xml. So not sure what Jenkins is saying it's going to reference. I'm going to try out just putting settings.xml here and see how that goes
I made sure that it was using my global settings file, and made that file match what was on my machine. Didn't need to touch my pom.xml at all.
I have created a RESTful API using jersey that serves some GET endpoints. The thing though is that now I have to ship this project as github link and provide instructions to open and run the project from the command line. I completely build the project using Eclipse and have scoured the web for resources but have no clue as to how to get this done. Could someone care to download the project into their machines and help with instructions to run it from the command line. The README has the links to the public endpoints.
Without needing to actually deploy it your Tomcat instance, you can use the tomcat maven plugin (which is meant for development). It starts an embedded tomcat instance, so you can test your webapps. Just add the following to your pom.xml file
<build>
<finalName>SimpleRestApi</finalName>
<plugins>
...
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
Then from the command line you can just run
mvn tomcat7:run
And a server instance will start. You can then access
http://localhost:8080/api/courses
If you want to change the context path, just use <path>/SimpleRestApi</path>. Then you can access
http://localhost:8080/SimpleRestApi/api/courses
I have an external library that needs to be dynamically linked with a test in my java project. The project is setup using maven, and I need to add the following to my vm arguments in eclipse for the test to pass:
-Djava.library.path=${env_var:HOME}/.m2/repository/natives/dist/lib -ea
Unfortunately this means that running the test from maven using: mvn test will always fail.
One work around is to call mvn with a -DargLine argument like so:
mvn test -DargLine="-Djava.library.path=/Users/rob/.m2/repository/natives/dist/lib -ea"
However, clearly this has the problem of being specific to my machine, so I can't put it directly in the pom file. I guess what I'm looking for is a way of modifying that string on a per machine basis kinda like the first line does for eclipse.
I'm also curious how I could put it into the POM file, I've tried placing it inside of <argLine> tags, but that doesn't seems to work, is there something I'm missing:
<argLine>-Djava.library.path=/Users/rob/.m2/repository/natives/dist/lib -ea</argLine>
After some research I've discovered a decent solution to my problem.
In maven your settings.xml file, you can define a location for the localRepository here are the defaults if you set nothing:
Unix/Mac OS X – ~/.m2
Windows – C:\Documents and Settings\username.m2
As you can see this matches at least the first part of the directory I was trying to set: /Users/rob/.m2
Since dynamic linking is OS specific, you may also want to setup a profile for alternate path suffixes. You can do this in a .pom like this:
<profile>
<id>OSX</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<dynamic.libLoc>${settings.localRepository}/natives/dist/lib</dynamic.libLoc>
</properties>
</profile>
You can then use this property in the .pom for the project you wish to test. Under the plugins category you can add:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Djava.library.path=${dynamic.libLoc}</argLine>
</configuration>
</plugin>
Now maven can run those tests without users having to specify the location of the dynamically linked libraries. You can also handle users with different operating systems by just adding another profile.
Note: With regards to my problem with <argLine> earlier. I think I was just using it in the wrong .pom