Exclude Reading/Replacing specific files in Maven Build - maven

We are using maven for building the project. It's legacy and huge one.
We newly added few .keystore files to it's resources folder.
The problem is, once the build is done, the .keystore files are getting tampered [may be maven is trying to replace/search for some placeholders]. Since it's legacy one, the project structure is so much messed up and we don't have separate distributions or no other choice but to go with plain build.
What I want is, tell maven to copy these sort of files without touching them and keeping the build as usual like before.
Between, there's no explicit is mentioned in pom.xml, tried to doing with that as per this http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html but it's messing up the project build.
I don't want to tamper the build, since it's legacy and huge one. We are using Ant plugin

Just switch off filtering for the respective <resource/> or add an <exclude/> for it.

After going through lot of sources, Found the solution http://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html
Thanks :)

Related

Why refresh of Maven repository is not enough for IntelliJ?

I had a NoClassDefFoundError problem with some test, launched from IntelliJ. In order to repair the situation, I had to make several changes in many poms of the project - adding new packages and excluding some old ones for to escape the overlapping of them. Also, I reapired the situation with different versions. But the situation did not improve. Again, some package, declared in pom, was not found where it should be.
I refreshed the maven repository by
mvn -e clean install -U
, as is advised in https://stackoverflow.com/a/9697970/715269 - so old and upvoted answer, that it surely looks as Santa.
The problem remained unchanged.
I output the maven map. It was correct and it contained all needed.
I looked at the list of the External Libraries of the project. It was the old uncorrected list of overlapping jars with same names and different versions, and without good packages I added just now, and well seen in maven tree output!
Already hapless,
I reimported packages in IntelliJ
by:
Ctrl+Shift+A, Reimport All Maven Projects.
Ho! The list of libraries got repaired. And the problem, mentioned in subj, disappeared.
The question is: How it could happen, that the same project has that very pom for everything, but gets packages differently being launched in maven and in IntelliJ?
I know about that feature "delegate IDE build to Maven". And I keep it turned off. But I am NOT talking about the different SW for building. Whether they are different or not, they should be up to the actual pom's. And whereas maven, if turned off from the automatic building won't know about changes in poms, IntelliJ KNOWS about them. It could have jars up to pom, or up to maven - it has sense, but it simply has some old rubbish. Was there some deep thought under that construction?
Every time you manually change the pom.xml file, including the dependencies you need to load these changes into IDE. IDE does it on Reload from Maven action. See also Import Maven dependencies.
Intellij doesn't use maven to bulid and run a project except you are delegating build and run action to maven:
Since, IDEA doen't really use maven to run and build, it uses the pom.xml to import the project structure and "tries" to build the project the same way was maven does.
Actually, there are quite a few differences between these to build processes.
Generating sources or filtering resources (don't know if this is still an issue) aren't done during building the project with Intellij IDEA.
In case you are using code generation you have to build the project via maven first and then - when all the resouces are filtered and additional sources are generated - you are able to run, debug aso. the project with Inellij IDEA.
That's an important thing to be aware of and that's the reason why maven and IntelliJ IDEA project structures might get out of sync.
You can enable the "Reload project after changes in build scripts" feature and select the Any changes checkbox to keep your project structure updated:
Why should you disable this feature anyway
If you are working on a build file (gradle or maven is not important) reloading the structure on any change can be very anoying. It's cpu intense, dependcies are fetched aso.
Therefore, I prefer to reload project structure only in case of an external change. This happens when pulling an updated version of the build file for example.

Can I use maven to add sources as part of my project?

I am using a dependency and I have some issues with it.
I could download the sources and include it as part of my project and then start modifying the source to help me debug my issue.
However, is there an easier way to do this, using maven ? I have the source-jars downloaded but I am not sure if I can then use these source jars and modify the code as well ?
I could in theory unbar the sources and add them to my source build path, but is there an easier way to accomplish this ?
I am using maven and IntelliJ.
Are you able to get a successful build of the dependency you're having issues with (in it's own project?).
If so, change the version, e.g. 1.2.3-CUSTOM-1, make the improvements, rebuild, and use it as a proper maven depdendency with <version>1.2.3-CUSTOM-1</version>.
This might seem a lot of work, but it's not really - you end up with a properly versioned jar.... having a "hacked" version of 1.2.3 jar is asking for all sorts of problems later.
On the plus side, you can share and deploy the -CUSTOM-1 jar if you need to, and you can keep versioning -CUSTOM-2, etc.
This is the "proper way" I would say.
As Thorbjørn Ravn Andersen wrote, source jars are read-only. You can setup debugger breakpoints in them, but you can't "write" in them
Either unjar the sourced jar or if you know it came from a git release tag, clone the original repository and checkout the correct tag
Make sure its sources compile
Bump up the version in case you know you are going to hack the source
On the right pane, use Maven projects / Plus icon to add sources and use the "m" Execute goal icon to build the sources in IDEA :

using pom.xml instead of iml files in idea

We are using IntelliJ idea in order to develop our product.
till now, we saved the iml/ipr files in a version control in order to share the same configuration among developers (and also between different versions of our product).
however, since idea changes those iml files a lot, it is an exhausted task to resolve its conflicts, and actually feels a bit wrong.
Moreover, idea 15 is no longer checks-out iml/ipr automatically.
I tried the maven-idea-plugin which suppose to generate the iml/ipr based on the pom.xml and it looks promising (except for some minor issues) but it's obsolete and there many stackoverflow posts advise against it.
I also undestand that the "correct way" to load our project is by loading the e pom.xml directly in idea.
The problem with that loading the pom.xml is that each user has to reconfigure idea (such as exclude directories, set the run configuration, plugins, maven version, JDK location etc.)
every time he download the code from the version control, which happens quite often.
So, my question is what is the best way to use intellij idea with our project.
my goal is that once a developer download the code he will be able run idea without manually configure a thing.
Thanks,
Meir
I am using Intellij 15.0.3 Community edition.
In our current project we do not add *.iml to Git.
For shure we do add pom.xml to Git.
I did not add any special plugins or configure idea in the way you described
(such as exclude directories, set the run configuration, plugins, maven version, JDK location etc.)
So I am using Idea out of the box, only the plugin 'Maven integration' is activated. I initially cloned the Git repository in a new folder.
In Idea I choosed File| New | 'Project from existing sources '
and selected the folder with the pom.xml.
Thats all, works perfectly. I can build in Idea or on the command line.
I can use Git in Idea or on the command line.
Everything works perfect.
So I can asure you that its absolutely possible without having *.iml under version controle.

Maven war plugin copy arbitrary files

I apologize that this is surely basic maven/war plugin stuff, but I'm totally not a maven user. I just have to hack this one thing into a project that is maven based.
What I need to do is to copy an (essentially arbitrary) directory and files into the root of my war. I need them to appear as resources available via HTTP when the war deploys.
I cannot simply put them in the "right place" in the source tree. This is because the files in question are actually the source files of my project. I realize this is a bit odd, but this is a documentation project, and I need to show the source and the effect all in the same war.
So, in the general case, how would I configure the maven war plugin to copy a given directory, along with it's contents, to the root of my war? (BTW, I have tried to make sense of the documentation of this tool, but it seems to be predicated on so much understanding of maven that it feels like I'll never understand it without learning maven first, and I'm a bit too pressed for time to do that just now!)
Many TIA
Toby.
You could try:
use the copy-resources plugin. Use the plugin to copy your source files under target prior to when the war is packaged.
or, configure the maven-war-plugin to include additional resources.
Thanks Drew, that got me where I needed to go. Bottom line, I added the sample pom fragment from the link you gave for copy-resources to my pom.xml with the following changes:
<outputDirectory>target/${project.name}-${project.version}/sources ...
<directory>src/main/java ...
I copied this from the link you gave, then edited the element to point at src/main/java, which picked up my files, and the outputDirectory to the aggregate target/${project.name}-${project.version}/sources . I found that ${project.name} mapped to the project, and the version came from ${project.version}, which seemed to resolve the last little bits of issue.
Thanks again.
Toby

How to easily copy/rename/remove files with Maven (as in Ant)

I am working on a project and using Maven to build it. The project is a quite big Java web application and it is supposed to work with both Mysql and Oracle databases.
The problem is that there are some specific annotations related to either of the two databases in the source code, plus some other differences, so that I am forced to manually comment/uncomment part of the code before building the application for one of the two databases.
Basically what I would like to achieve is to have my build script, maybe via a Maven profile, to automatically switch the source classes before building depending on the database I want my war to work against.
Putting it simply, the idea is to have MyClass.oracle and MyClass.mysql, and depending on my build profile I should move one of the two in the source dir, rename it MyClass and build. This should be done for some packages, classes, and also configuration files.
Is there any way I can achieve it via "pure" Maven? The only solution I came across till now is to use an antrun plugin and reference an Ant build.xml inside of it.
Thank you,
Mattia
A pure maven solution would be to develop your own maven plugin. Depending on your requirements this can be an overkill, however it is not hard at all, you can see how to achieve this here.
This is a limitation of Maven. One of Maven's purposes is to not have a build script. You should simply use the plugins as available, and setup your project the right way, and magically, everything will build!
There is one solution: Use Ant. Well, not to redo your whole project with Ant, but with the antrun plugin, you can run a few Ant tasks at various phases of your Maven build life cycle.
It's been a long, long time since I've used this, so I am not going to try to write a test pom.xml, but I don't remember it being very difficult to use.
Of course, the correct Maven solution is to divide your project up into "common core" code, and then a separate Oracle and MySql client that uses the "common core". By the way, I hope you're not patching source code. Instead, you're using a properties file to do this for you.

Resources