maven deploy exclude - deployment

I want to exclude one dependency when I'm doing deploy, I need it when I use the jar locally, but it should not be in deployed jar. is there any way to do it?

It depends on what you mean by "use the jar locally".
If you mean that you don't want the jar to be included in any bundles, you can set the scope of the dependency to provided. This scope is only available on the compilation and test classpath, and is not transitive. A dependency with this scope will not be included in wars/ears.
<dependency>
<groupId>some.groupid</groupId>
<artifactId>my-dependency</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
If you mean you don't want the jar to be bundled into a distribution built with the assembly plugin, you can configure the assembly to exclude a specific dependency.

<profile>
<id>localProfile</id>
<activation>
<property>
<name>!deploy</name>
</property>
</activation>
<dependencies>
<dependency>
...
</dependency>
</dependencies>
</profile>
when i run it with
mvn deploy -D deploy=0
the jar doesnt have that dependency

Maybe a good solution is to set the dependency with a <scope>provided</scope>. This way, the dependency will not be deployed.
However, locally, you will need to add the dependency in your classpath...

Related

Eclipse Maven dependency jar grayed out, can't import classes from it

I'm helping a friend configure a maven project with m2eclipse for the first time. We're both pretty unfamiliar with it and are encountering an issue where even though a dependency jar is showing up with packages in it under "maven dependencies" in the Project directory, if we try to import anything from any of that jar's packages, it can't find the class.
I noticed that the jars that are having issues are gray and not as opaque as the rest of the jars that are working.
What's strange is if you hover of the class name in the import, it shows a brief description of the class (from the documentation in the jar!) but it won't let me import it. All the other maven dependencies can be imported fine. Any ideas? We can't seem to even find what the darker icon means.
Also, the pom.xml is dead simple:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.something.portal.test</groupId>
<artifactId>PortalFrontEndTests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>PortalFrontEndTests</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.1</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I'm not sure what I'm missing here
open your pom.xml file
check for the name of the grayed out jar file
change
<scope>test</scope>
to
<scope>compile</scope>
I found the issue. It was because I had the class in the source directory instead of the test directory and both of the maven dependencies had been marked as "Visible only to test"
I had the same problem when i used the <scope>test</scope> in the maven pom.
It seems as if the newer Eclipse/Java versions do have a new Attribute :
<classpathentry kind="src" output="target/test-classes" path="src/test/java/...">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
This should be enabled in the Java Build Path Settings:
Image showing "Containts test sources" option from build path menu
After enabling this i got rid of all the compiler errors.
check for your dependency scope in POM file
compile, provided, system and test these were the available test
test -> compile would change your dependencies from grey to white.
If your dependency is for test scope then that dependency is not available for normal use in application whereas compile scope sets that dependency in class path of your project.
I am not sure on the grayed out part. If this is the feature because it suggest that Testing class should be under /test rather /src.
However, solution to your problem is scope of plugin, change it to compile and you will be good to go.
i.e. replace test with compile:
<scope>test</scope>
<scope>compile</scope>
That's it. you will not get any error for import testing packages.
Just removing the Scope will work. I tried as the following:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0-M1</version>
</dependency>
I am also faced the same problem
set scope to compile or remove scope
open maven dependencies
right click on dependency and click download resources

How to resolve a pom in the dependencies list?

I am very new to Maven and am trying to use it to load the JARs I need for a web project.
I have been using the goal dependency:copy-dependencies and have set the output path to <project>\WEB-INF\libs. This has worked well so far.
I now want to intergrate the latest version of Swagger which has the following POM:
<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-project -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-project</artifactId>
<version>2.0.0-rc4</version>
<type>pom</type>
</dependency>
All this does is to load the POM to the libs folder.
Is it possible to recursively resolve POMs in the dependencies list or have I misunderstood something basic?
As has been said in the comment, take out the pom:
<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-project -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-project</artifactId>
<version>2.0.0-rc4</version>
</dependency>
The default type is jar so you don't need it, other types are pom, war, ear.
Make sure you have your proxy set in your settings.xml, which should be in ~/.m2 directory.

How to integrate JUnit 5 with an existing Eclipse project?

The existing Eclipse project uses Maven but does not know about JUnit. Should/can I integrate JUnit into the existing project or should I make a new project dedicated to JUnit or is there a better option?
You can add JUnit5 to that project by including the following dependencies in the pom.xml:
<properties>
<junit.jupiter.version>5.0.1</junit.jupiter.version>
<junit.platform.version>1.0.1</junit.platform.version>
</properties>
<!--
JUnit5 dependencies:
* junit-jupiter-api: for writing JUnit5 tests
* junit-jupiter-engine: for running JUnit5 tests
* junit-platform-xxx: the foundation for JUnit5
* (Optionally) you might want to include junit-vintage-engine for running JUnit4 tests
-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
To enable Maven Surefire to run JUnit5 tests just include the following plugin definition in the pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<excludes>
<exclude>**/MongoPopulatorTool.java</exclude>
</excludes>
</configuration>
<dependencies>
<!-- integrates JUnit5 with surefire -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<!-- ensures that a JUnit5-aware test engine is available on the classpath when running Surefire -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
</dependencies>
</plugin>
And, finally, to enable Eclipse's test runner to run JUnit5 tests you must be running Eclipse Oxygen.1a (4.7.1a) release (or later), have a look at the Eclipse docs.
The other answer gives the technical answer how to add JUnit to your project setup.
But sorry, the real answer is: don't add your unit tests to your other project. Create a new one instead.
One of the most important rules when doing development is the Single Responsibility Principle. Any class/method/xyz should be doing one thing.
In other words: your existing eclipse project has the responsibility to provide the context for your "product". Providing context for testing is a different responsibility.
And beyond that, you should always follow "best practices". And best practice is again to not have test and production code within the same project.
You see, you absolutely do not want that your test source code sits in the same directory as your production code. Therefore, you have two projects, that can both use the same packages - but have their source code sitting in different folders!
( the reason why you don't want to have that: you only want to allow your tests to depend from your production code. but when files sit in the same directory, you might inadvertently create dependencies in the other direction )

How do I use a maven BOM (bill of materials) to manage my dependencies in SBT?

I want to use an external BOM to manage dependency versions for my project in SBT.
For example, the AWS Java SDK publishes a bill-of-materials artifact to their maven repository: https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-bom/1.11.86
I can use it to manage versions of dependencies in the AWS SDK. In Maven I can do this by adding the BOM to my <dependencyManagement> section like so:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.86</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Then when I want to use a module that's covered in the BOM I can omit the version and the BOM will resolve it for me:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sns</artifactId>
</dependency>
</dependencies>
Similarly in Gradle, I can use the BOM to manage dependencies for me using this plugin, like so:
apply plugin: "io.spring.dependency-management"
dependencyManagement {
imports {
mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.86'
}
}
dependencies {
compile 'com.amazonaws:aws-java-sdk-sns'
compile 'com.amazonaws:aws-java-sdk-s3'
}
Is there a similar plugin for SBT?
I'm looking for the same and have searched in a lot of place.
Most interesting thing I found is it looks like there is Open Ticket on SBT Project:
https://github.com/sbt/sbt/issues/4531
Can't wait that it's resolved !
Have you tried to use Ivy with sbt? It allows you to specify "get latest" by using rev="+"
<ivy-module version="2.0" xmlns:m="http://ant.apache.org/ivy/maven" xmlns:e="http://ant.apache.org/ivy/extras">
<dependencies>
<dependency org="com.amazonaws" name="aws-java-sdk-s3" rev="+" conf="compile->compile(*),master(*);runtime->runtime(*)" />
</dependencies>
</ivy-module>
See http://www.scala-sbt.org/1.0/docs/Library-Dependencies.html
If I understand you correctly, you can add this to your libraryDependencies:
"com.amazonaws" % "aws-java-sdk-bom" % "1.11.800" pomOnly()
You still have to put that version number in a variable and use it with the SDKs you actually want, unless someone knows the right magic to use to use in the revision field. I know you can go latest.release if you want the latest release version.

Maven Dependencies references non existing library

As stated above I have a problem regarding maven. In my current eclipse project I used maven for dependecy management and it worked fine until now.
The project is not working anymore and it states that:
Maven Dependencies references non existing library:
c:\.m2\repository\org\eclipse\lyo\oslc4j\core\oslc4j-core\1.1\oslc4j-core-1.1.jar'
If i check the folder there are files that have the same name as the jar but end on jar.lastUpdated.
I tried maven clean and maven update which did not work. The line in my pom.xml is marked red and states:
Missing artifact org.eclipse.lyo.oslc4j.core:oslc4j-core:jar:1.1
This is the content of my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/www.w3.org2001XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.or/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rtc_oslc_common</groupId>
<artifactId>rtc_oslc_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
This block is the one with the marker where it states the artifact is missing.
<dependencies>
<dependency>
<groupId>org.eclipse.lyo.oslc4j.core</groupId>
<artifactId>oslc4j-core</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
I don't know why it is not just reloading the jars and instead stays in this weird state. Help very much appriaciated.
Basically Maven is telling you that it cannot find the dependency you declared in your pom.xml. The fact that there is a file called *.jar.lastUpdated means that Maven tried to download the dependency but wasn't able to fetch it.
First you could try to completely delete the folder containing the *.jar.lastUpdated and run the build again. Maven will then try to download the dependency again. Perhaps it was just a temporary error.
But as this dependency is not available in Maven Central, you may have to add the corresponding repository to your pom.xml.
Sometimes an officially stated dependency configuration is incorrect, too. For example, as of 2015-05-23 the Robolectric site says...
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>robolectric</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
... which gives the same error you describe. After banging my head for a while (of course this was the first time I've ever tried Maven), I ended up checking at Maven Central this URL: http://mvnrepository.com/artifact/org.robolectric/robolectric
Lo and behold, there is no version 3.0. Instead, there are versions "3.0-rc1" and "3.0-rc2". When I changed the dependency to "3.0-rc2", suddenly everything worked.
I got the error like this:
The container 'Maven Dependencies' references non existing library 'G:\m2\repository\org\apache\tomcat\tomcat-embed\7.0.65\tomcat-embed-7.0.65.jar'
I found the tomcat-embed is actually a *.pom file but not a *.jar
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-embed</artifactId>
<version>7.0.65</version>
<type>pom</type>
</dependency>
So I fixed this problem by:
delete tomcat-embed-7.0.65.jar.lastUpdated
copy a *.jar file to dir G:\m2\repository\org\apache\tomcat\tomcat-embed\7.0.65\
renamed to tomcat-embed-7.0.65.jar
But the other way to solve this problem?
Firstly, you repair, intenet problem. after that You must delete folders in maven repository folder. (For me C:\Users\ekomut.m2\repository)
If you have an Internet problem, you can see "*.jar.lastUpdated" .If you don't delete this folder, maven can not try download again.