Make maven build fail created artifact contains certain classes - maven

I have complex multi-pom setup that in the end will create an EAR package. Sometimes the dependencies are messed up and some unwanted dependencies like JUnit end up in the final EAR package.
Is there a way to make maven build fail if final artifact contains certain classes?

Maybe not failing but you can exclude some jar-files that you know you will never use.
The maven-ear-plugin has a tag to use when excluding files, look here.
Use the <packagingExcludes/> tag to exclude JUnit for example.

You could use the maven-enforcer-plugin with a custom rule that you probably would have to write yourself. I haven't searched, but I don't think that such a rule exists yet.

Related

Assembling a jar using Maven containing only files in a specified folder

I have the following Maven project structure.
project
-- src
-- main
-- java
-- models
-- resources
I want to create and deploy a jar project-models.jar containing everything inside the folder models and nothing else. Since I'm not very familiar with Maven, I'd really appreciate if you could provide me some example.
models belongs to resources (They should neither be compiled nor tested, should they?)
See How to create an additional attached jar artifact from the project:
Specify a list of fileset patterns to be included or excluded by adding <includes>/<include> or <excludes>/<exclude> and add a classifier in your pom.xml.
Note: the jar-plugin must be defined in a new execution, otherwise it will replace the default use of the jar-plugin instead of adding a second artifact. The classifier is also required to create more than one artifact.

Gathering javadocs from multimodule maven project

In a large maven multimodule context,
how can I gather javadoc-comments programmatically for a specific set of classes (e.g. all classes implementing some interface) or modules ?
I have tried a stupid doclet and looked at QDox, but neither seems to do the job well.
Actually I think this should be simple if done correctly.
Specifically, I do not know how to do this in a maven-build: How can I depend on and use the src-jars?
This should be possible with QDox, as long as you have the sources. QDox-2.x can also read source-files from jars, which can be generated by the maven-source-plugin.

Understanding Maven scoping better

I have been struggling to figure out what's the use of scoping that is provided by Maven
as mentioned here.
Why should you not always have compile time scoping? Real life examples would be really appreciated.
The compile scoped dependencies are only used during compilation.
The test scoped ones -- only during tests. Say you have tests using junit, or easymock. You obviously do not want your final artifact to have a dependency on them, but would like to be able to just depend on these libraries while running your tests.
Those dependencies which are marked provided are expected to be on your classpath when you're running the produced artifact. For example: you have a webapp and you have a dependency on the servlet library. Obviously, you should not package it inside your WAR file, as the webapp container will already have it and a conflict may occur.
One of the reasons to have different scopes for dependencies is that different parts of the build can depend on different dependencies. For example, if you are only compiling your code and not executing any tests, then there is no point in having Maven downloading your test dependencies (if they're not already present in your local repository, of course). The other reason is that not all dependencies need to be placed in your final artifact (whether it's an assembly, or WAR file), as some of the dependencies are only used during the build and testing phases.
compile
Will copy these jar files into prepared War file.
Ex: hibernate-core.jar need to have in our prepared War.
provided
These jars will be considered only at complie time and test time
Ex:
servlet.jar will be provided by deployed server, so no need to provide from our prepared War file.
test
These jars are only required for running test classes.
Ex: Junit.jar will be required only for running Junit test classes, no need to deploy these.
Scopes are quite well explained in here:
https://maven.apache.org/pom.html#Dependencies
As a reference, I copied the paragraph:
scope: This element refers to the classpath of the task at hand
(compiling and runtime, testing, etc.) as well as how to limit the
transitivity of a dependency. There are five scopes available:
compile
- this is the default scope, used if none is specified. Compile dependencies are available in all classpaths. Furthermore, those
dependencies are propagated to dependent projects.
provided - this is
much like compile, but indicates you expect the JDK or a container to
provide it at runtime. It is only available on the compilation and
test classpath, and is not transitive.
runtime - this scope indicates
that the dependency is not required for compilation, but is for
execution. It is in the runtime and test classpaths, but not the
compile classpath.
test - this scope indicates that the dependency is
not required for normal use of the application, and is only available
for the test compilation and execution phases.
system - this scope is
similar to provided except that you have to provide the JAR which
contains it explicitly. The artifact is always available and is not
looked up in a repository.
there are a couple of reasons that you might not want to have all dependencies to be default compile scope
reduce the size of final artifact(jar,war...) by indicating different scope.
when you have a multiple-modules project, you have ability to let each module have it's own version of dependency
avoid class version collision by provided scope, for instance if you are going deploy a war file to weblogic server, you need to get rid of some javax jars, like javax.servlet, javax.xml.parsers, JPA jars and etc. otherwise you might end up with class collision error.

Multiple reusable modules in maven

I have a couple of java modules set up in IDEA and I am wanting to mavenize them. These java modules use classes from one another.
I was not quite sure how I should take up this and I decide to add modules on a maven project using IDEA. Hence first I created a maven project, let's name it pm1 which has a class let's name it TempClass1. Now this class can be used in other maven project. Hence I added another maven module - pm11 and tried to use TempClass1 with in pm11. It worked and I notices that IDEA had added module dependency of pm1 in pm11. So whole structure looks as -
But now when I do mvn test from pm11 then it fails with error message package package1 does not exist and it looks to me that it is because package1 is in a different maven project. And I am not sure how I could use classes which reside in a different maven project. I hope I am clear in my question.
You can use classes of other maven projects, as long as there's a proper maven dependency defined in pom.xml. Ensure that the dependency is defined and its' scope is either undefined or relevant (You may have problems if the scope is provided for example).

Any way to strip off some annotations by maven?

I wonder if there a way to strip off some annotations when 'maven build' ?
For convenience reason , I annotate some 3rd party's annotation into my POJO , such as javax.persistence.* , org.hibernate.annotations.* , org.apache.lucene.analysis.* ... blah...blah.
It works well when I bundle them together and deploy to a JavaEE/Spring server/container.
But when developing Android , I want to make the most reuse of my code/POJOs. I want to directly import foobar.core.User instead of creating another foobar.android.User without 3rd party annotations. That is , I don't want the jar dependent on other spring/hibernate artifacts.
So , is there any way to strip off some annotations when running 'maven build' ? I just need to strip off 'some' 3rd party annotations , and keep custom core annotations.
So that I can build foobar-core.jar and foobar-core-stripped.jar respectively or simultaneously.
Trying to do exactly the same thing. I found an ant task that can strip annotations from compiled classes.
http://sourceforge.net/projects/purgeannorefs/
The maven ant plugin can then be used to call the ant task as part of your maven build.
I know it's a bit of a workaround. But better than having to maintain 2 copies of the pojo objects.
If you can't find a non-maven tool that does the job, you aren't going to find a maven plugin. Mmake stub copies of the annotation classes put them in a jar, and add them into the classpath for Android.
You can use the maven-antrun-plugin to do a find and replace on all annotations... I did this at the following answer to comment out all #XmlAttribute annotations:
https://stackoverflow.com/a/10036841/342745
Kind of hacky, but it worked for me.

Resources