jackrabbit: Why do i get javax.jcr.RepositoryException: Unable to access a repository when I execute a runnable jar file and not on netbeans? - maven

I have created a runnable jar file from an existing maven project on netbeans,
which contains all dependencies included in pom.xml
When I run it on Netbeans it work:
stampa
adsa
stampa2
when instead I run it from the runnable jar file I get this error:
java -jar ./Prova-1.0-SNAPSHOT-jar-with-dependencies.jar
stampa
adsa
Exception in thread "main" javax.jcr.RepositoryException: Unable to access a repository with the following settings:
org.apache.jackrabbit.repository.uri: http://localhost:8082/server
The following RepositoryFactory classes were consulted:
org.apache.jackrabbit.core.RepositoryFactoryImpl: declined
Perhaps the repository you are trying to access is not available at the moment.
at org.apache.jackrabbit.commons.JcrUtils.getRepository(JcrUtils.java:223)
at org.apache.jackrabbit.commons.JcrUtils.getRepository(JcrUtils.java:263)
at com.mycompany.leggitutto.Source.main(Source.java:38)
I don't get it.
Why on netbeans the RepositoryException is not thrown?
The java code is the same, the build is successful and the Run is different!!!
public static void main(String[] args) throws Exception
{
System.out.println("stampa");
System.out.println("adsa");
Repository repository1 = JcrUtils.getRepository("http://localhost:8082/server");
Session session1 = repository1.login(new SimpleCredentials("admin","admin".toCharArray()), "default");
System.out.println("stampa2");
}
Jackrabbit server is running at
"http://localhost:8082/server",
I have even checked it on firefox, and the repository is reachable.
I'd be glad if someone can help me to figure this out :)

I solved it,
I changed maven plugin in pom.xml in onejar-maven-plugin and It works now.
Appearently, using maven-shade-plugin or maven-assembly-plugin, not every dependencies were added to the final jar "executable".
This pom.xml is working YEAH!!!
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<mainClass>Valid.Main.Class</mainClass>
<!-- Optional -->
<onejarVersion>0.97</onejarVersion>
<!-- Optional, default is false -->
<attachToBuild>true</attachToBuild>
<!-- Optional, default is "onejar" -->
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>

Related

Jenkins SonarQube plugin Multi Module Code Coverage Not Displaying

so we have a Spring Boot maven based project, which we split into multi modules which all works perfectly fine in unit tests and Jenkins, but coverage is not showing up in Sonar at all.
This is the structure of our application:
ApplicationRoot
-SharedCommonModule
--main
---java
-----com...(SomeModule.java)
--test
----com....(SomeModuleTest.java)
-ApplicationModule
--main
---java
-----com...(Application.java)
--test
----com....(ApplicationTest.java)
Parent pom file config:
<properties>
<!-- Sonar -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.language>java</sonar.language>
<jacoco.destFile>${sonar.jacoco.reportPath}</jacoco.destFile>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
SharedCommonModule pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Main ApplicationModule pom file:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.org.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Jenkins SonarQube plugin configuration:
sonar.projectKey=com.Application
sonar.projectName=ApplicationModule
sonar.projectVersion=1.0.0.${BUILD_NUMBER}
sonar.sources=src/main
sonar.tests=src/test
sonar.java.binaries=target/classes
sonar.jacoco.reportPaths=target/jacoco.exec
sonar.modules=ApplicationModule,SharedCommonModule
ApplicationModule.sonar.projectName=ApplicationModule
SharedCommonModule.sonar.projectName=SharedCommonModule
We have researched and tried to hack it together from multiple examples, but nothing seems to work - closest we've got, is for Sonar to show some coverage, while some classes would show 0% coverage even though we know for sure we have UTs that used those classes (tested via IntelliJ).
So, without without the added properties and build xml sections above, we get partial coverage, only for ApplicaitonModule, I think all reported uncovered classes, belong to SharedCommonModule
EDIT: I want to clarify, the combined jacoco.exec file does show coverage for classes when loaded in IntelliJ Coverage tool, but Sonar does not show coverage for the very same classes in its report (which is generated only when I remove the build and properties xml elements in the parent pom).
Please help :)
You have Maven projects, so you should start using Sonar Scanner for Maven.
It is smart enough to generate all parameters for you.
If you remove:
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<jacoco.destFile>${sonar.jacoco.reportPath}</jacoco.destFile>
<destFile>${sonar.jacoco.reportPath}</destFile>
Jenkins SonarQube plugin configuration
add to parent pom file:
<name>ApplicationModule</name>
<properties>
<sonar.sources>src/main</sonar.sources>
<sonar.tests>src/test</sonar.tests>
<sonar.projectKey>com.Application</sonar.projectKey>
</properties>
add to SharedCommonModule pom file:
<name>SharedCommonModule</name>
add to ApplicationModule.pom file:
<name>ApplicationModule</name>
and finally execute:
mvn sonar:sonar -Dsonar.projectVersion="1.0.0.${BUILD_NUMBER}"
After that you should see missing coverage data.
Btw. it is not recomended to set sonar.projectKey for Maven projects. I set it to the same value, so your project will be accessible under the same link.

When does a Maven plugin uses the POM in the current directory?

I use the Versions Maven Plugin to check for updates of my dependencies. Therefore I added the following lines to my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>${versions-plugin.version}</version>
<configuration>
<rulesUri>classpath:///rules.xml</rulesUri>
</configuration>
<dependencies>
<dependency>
<groupId>versionrules</groupId>
<artifactId>versionrules</artifactId>
<version>1-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
But this configuration is not used if I run the Versions Maven Plugin from the commandline in the same directory as the pom.xml. The only way to use my own configuration is to put this plugin configuration in a profil and execute this profil during the Maven run.
Is there a way to run the Versions plugin on the commandline and to configure it via the pom.xml? I am sure my questions does not only apply to the Versions plugin, but to any Maven plugin.
This can be done by using an execution id default-cli in your execution definition the configuration will be used during the execution on command line (using the current configuration) furthermore since Maven 3.3.1 you can use things like:
mvn version:set#second-cli
which means you can do a different configuration for command line in the pom file:
Just by simply separating them by different id
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.5.</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
...
</configuration>
</execution>
<execution>
<id>second-cli</id>
<configuration>
....
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
So this means you can have different configurations for running on command line by giving the id.

OSGi Code coverage - Jacoco + Easymock

I'm currently working on a Java OSGi project(based on Apache felix runtime) with a project setup like the one below:
pkg | parent maven project
persistence | real plugin
persistence.tests | test plugin (indeed a Fragment project with fragment host the persistence plugin above )
... others like the ones above
Basically I use maven + tycho to manage the lifecycle of the project. the whole stuff flows through a continuos integration pipeline which involves jenkins for building, testing, deploying and forwarding code analysis to a Sonarqube server. Just like mentioned above, tests are implemented through Fragment projects pointing OSGi bundles to be tested. In these tests I make use of EasyMock library to generate mocked OSGi bundles.
In order to make Sonarqube aware of tests coverage I had to add Jacoco (maven plugin) into my sets of tools. After a few adjustments to the configuration of my parent pom.xml file I ended up with something that is working partially: jacoco code coverage is only working for classes included in test plugins (the fragment projects). As you may guess - though better than nothing - this result is far from being useful. I need to evalute test coverage on real OSGi bundles. After some googling I understood that the problem could be linked to the usage of EasyMock library, since this alterate original classes during execution causing a mismatch between test classes and real classes. According to my understanding, to solve I need to disable jacoco on-the-fly instrumentation and use offline instrumentation instead.
Nevertheless I'm not able to understand :
what does this really means
how to do it
Can someone kindly revert on this ?
This is the maven command i'm running to generate jacoco report
mvn -f com.mycompany.osgi.myproject.pkg/pom.xml clean test
Below my current parent 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>com.mycompany.osgi</groupId>
<artifactId>com.mycompany.osgi.myproject.pkg</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<properties>
<tycho.version>1.0.0</tycho.version>
<surefire.version>2.16</surefire.version>
<main.basedir>${project.basedir}</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jacoco.version>0.7.9</jacoco.version>
<!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>${project.basedir}/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>
<modules>
<module>../com.mycompany.osgi.myproject.core.persistence</module>
<module>../com.mycompany.osgi.myproject.core.persistence.tests</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<configuration>
<testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
...
</repositories>
<distributionManagement>
...
</distributionManagement>
As suggested by #Godin, my problems were solved using the following jacoco plugin configurations
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<dataFile>../com.mycompany.myproject.pkg/target/jacoco.exec</dataFile>
<destFile>../com.mycompany.myproject.pkg/target/jacoco.exec</destFile>
<outputDirectory>../com.mycompany.myproject.pkg/target/site/jacoco</outputDirectory>
</configuration>
</plugin>
and this project configuration to instruct sonarqube to read expected resources
<properties>
...
<!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>com.mycompany.myproject.pkg/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>com.mycompany.myproject.pkg/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>

How To Use The Sonar Maven Plug-in

Easy question here. I want to add sonar to be executed on every Maven build. I tried:
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.1.1</version>
</plugin>
and
<plugin>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>5.1</version>
</plugin>
and
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.7.1</version>
</plugin>
because a) I couldn't figure out what the plug-ins do and/or b) which one is the current one.
If I only add the above to <build> -> <plugins> it's not executed ever (so the plug-in doesn't have a default execution). So of course I added a <execution> instruction, and after that Sonar gets executed, but with the following error message:
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.1.1:sonar (default) on project org.acme.project.build: Can not execute Findbugs: This project contains Java source files that are not compiled.
It does not seem to matter which phase I use (I tried validate and compile and test and prepare-package and package even though not all of them make sense). I am sure there is no source code generation anywhere in the project. And the static classes get compiled just fine.
I think the problem might be that the plug-in gets executed for every module, including the parent pom project. Which is weird, because sonar:sonar skips that project.
But the project structure is simple and I can't find anything unusual about it:
<groupId>group</groupId>
<artifactId>org.acme.project.build</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>org.acme.project</module>
</modules>
<profiles>
<profile>
<id>sonar</id>
<properties>
<sonar.host.url>http://sonar.acme.org/</sonar.host.url>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The project org.acme.project has nothing besides its own artifact ID and the parent. The command line is: mvn clean deploy -Dsonar.login=Wile.Coyote -Dsonar.password=*********** -Psonar
The log shows that sonar is always executed before the install phase, which of course is way to early.
So how do I use Sonar's Maven plug-in to analyze my code?
a) I couldn't figure out what the plug-ins do
The plugin is used to gather the details from code coverage reports and the repository code scanning for getting to analyze possible bugs, duplications etc. You can search for a sample sonar report to find what all and how to get these details with maven using two methods like settings.xml and maven plugin is detailed at SonarQube Scanner for Maven and
SonarQube - analyzing with Maven
b) which one is the current one.
The maven central suggests that the current plugin from org.codehaus.mojo used as
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
has been moved to
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
So you should ideally be using the one from groupId - org.sonarsource.scanner.maven as also suggested by the SonarQube Docs
Also the artifact from org.codehaus.sonar version 5.1 seems to be outdated and not maintained.

Error while trying to use an API. java.lang.NoSuchFieldError: INSTANCE

I am trying to connect to the smartsheet api using a java program.
Initially I had problems with the site certificate which was resolved by adding it to the java keystore. Now when I am trying to run my code, I get the following error.
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
at com.smartsheet.api.internal.http.DefaultHttpClient.<init>(DefaultHttpClient.java:64)
at com.smartsheet.api.SmartsheetBuilder.build(SmartsheetBuilder.java:203)
at SmartsheetConnection.main(SmartsheetConnection.java:13)
This is my code (I followed their documentation).
import com.smartsheet.api.*;
import com.smartsheet.api.models.*;
import com.smartsheet.api.models.enums.*;
import com.smartsheet.api.oauth.*;
public class SmartsheetConnection {
public static void main(String[] args) throws SmartsheetException {
// Set the access token.
Token token = new Token();
token.setAccessToken("foo");
Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken(token.getAccessToken()).build();
}
}
The line that is throwing error is (line 144)
#Deprecated
public static final X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER
= AllowAllHostnameVerifier.INSTANCE;
but I am not sure what to make of it. I am using maven to get the dependencies. Does it have something to do with the version of the Apache HttpComponents?
Here is the pom.xml
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.smartsheet</groupId>
<artifactId>smartsheet-sdk-java</artifactId>
<version>2.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
Other posts about this error seem to suggest that it's typically caused by conflicting versions of httpcore jar. i.e., an older version of httpcore on the classpath.
For more information, I'd suggest you checkout the following posts:
java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application
java.lang.NoSuchFieldError: INSTANCE
I know its I am replying a bit late actually I am also struggling the same problem and I found the solution by using Maven Shade plugin.
The Problem is the JAR conflict probably your project is using a different Version Of HTTPclient then your container over which your Appliaction is running.
To resolve this use the Below Maven Shade Plugin which will change the package name of HttpClient to the specified one which packaging the JAR. This will also refactor all the usage in your code.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.apache.http</pattern>
<shadedPattern>org.shaded.apache.http</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
The Above sample will change HttpClient Package with org.shaded.apache.http from org.apache.http
Maven Shade will also create a fat/uber jar so your final package size get increased and will have all the classes which you have mentioned in the Dependency in POM.
If you don't want to include the all your dependency jar in your final jar then add the Scope for the Dependency as <scope>provided</scope>.
the reason for this problem is:
org.apache.http.conn.ssl.AllowAllHostnameVerifier class,which is execused in the runtime,has no field 'INSTANCE'.
My project classpath contains two same name class of "org.apache.http.conn.ssl.AllowAllHostnameVerifier".
One cames from a jar customized by our company,which has no field 'INSTANCE'.
Another cames from maven central repository,which has the field 'INSTANCE'.
My code sometimes run the logic of the latter jar and sometimes the fromer jar,which is the reason I guessed.
my classpath search result
the comparison of the two jar
I was using intellij for both android and spring development.
In my case, I accidentally chose Android sdk as the module SDK.
After choosing JDK 1.8 and rebuilding the project fixed the issue for me

Resources