Java config.properties file in Git - java

I have a Java Project which includes a config.properties file, in which this config.properties file has property values of paths to some executable files.
I've configured a Git Repository (using EGit eclipse) tool, but the application is simply not working due to the fact that the config.properties file is not included in the Git Repository (in gitignore) since all users who will push/pull to Git will have a different config.properties file. How shall I solve this problem? Is there a way to map the config file without including it to the repository.
Any help is highly appreciated, and I'm quite sure theres a solutions to this problem. 'Thanks in advance.

A way could be to gitignore config.properties, and add a config.properties-template with all parameters and comments explaining how to fill them if needed.

Solved. What I did was to include a piece of code whick will create a config.properties file with default values if none are found. –

Related

Deployed spring boot jar file to Docker - how to access and change static resources

I'm kinda new to spring and web development as a whole.
My question is:
When you build a spring boot project (using Maven) into jar file and deploy it via Docker, everything is in one jar file. How can you access your resources (css, js, images, html...) if you want to edit something? Like change something in css file or add something to html page. Is it even possible? Or do you have to build a new jar file everytime, when you need to change something (in frontend)? Also, when there are being uploaded some images or other files, where are they stored? This stuff is very confusing for me and i can't find any related books or help at all.
Thanks for help!
when you package any java program it is nothing but a zip file. Based on what kind of package it is, you wither name it as a Jar or War.
Jar == Java archive
War == Web archive
Now, given the fact that jar and war both are essentially a zip archive, it gives you flexibility to extract and modify them just like any other zip file.
On windows, I think softwares like 7zip let you update the jar inline. I have done it multiple times, especially when I wanted to change application.properties alone on cloud machines, and no other code changes were required. In such cases, building the whole jar and transferring it again to cloud machine could be time consuming. So I would just extract the contents, update whatever I want to, and rezip the package.
Here is the commands you can use -
jar xf jar-file
This should extract the files into a directory.
This SO thread will guide you towards creating jar files.
Something like jar cf myJar.jar ** should be enough to generate a jar file IMO, but syntax might vary.
The jar file is actually just a zip file containing all the files and classes of your application, so technically you can change files in it like any other zip archive. Best practice is to build the jar file using Maven or Gradle from source every time you need something changed.
It's good practice to keep the source in version control using Git, and tag each build in the git repository - that way you can easily keep track of changes to the jar file by looking at what's in git at the time of the build.

Copy properties to modules in multi-module project

I need to put my own properties (many different) to many modules. The properties contain sensitive data (DB passwords for example), so I cannot put it to my git repository. For now, I have 2 Jenkins jobs:
First clones my "project" and builds it.
The second clones my other repository that contains only properties and some .sh files. Then I copy properties from the second project and paste it into my .jar file.
The problem is that I have to "unzip" (or "unjar"?) the jar file, then paste properties, and after that "jar" the file again. Not so clear.
Good thing about that is if I want to change my properties I don't have to rebuild all project. Just run the second job and that is all.
I found some other options:
Jasypt
So I can encrypt my properties. Then I can store it in my repository. Not bad, but I do not see what password is stored. Next question - when I want to change something in my properties I need to build all over again. So Jasypt is a good idea to make your sensitive data safer but did not resolve my problem.
maven-remote-resources-plugin
I don`t know it at all, but maybe it is a better way to paste my properties into my project?
Maybe There are other ways to do that? Or maybe now I am doing it how it should looks? Btw. I am using spring boot 2.0.3.

How to Make Java .jar files be automatically found in any environment

I am trying to add an external library to my Java project. The library files are in .jar format, and every time someone tries to run the project in a different environment, they have to modify the build path so that their environment can find the .jar files.
Is there any way to make it so that the .jar files are automatically found by the build path? Maybe by specifying where in the project the .jar files are located?
Edit: Here is an example -
In my linux environment, the .jar files are located in:
/home/MyUsername\SomeDirectory\workspace\java_project_name\data
In my windows environment, the .jar files are located in:
C:\MyUsername\SomeDirectory\workspace\java_project_name\data
In the example above, the project root directory is "java_project_name", and the .jar files are always in the java_project_name\data folder. Even though this is the case, different environments can't seem to detect that it is always in the same path in relation to the project's root directory.
I have looked in many places on how to do this, and some people told me it isn't possible. This doesn't sound reasonable to me, which is why I am posting this question here.
Thanks in advance for your help!
you can add a ClassPath: entry in your manifest file of the main jar, and use relative paths. As long as the jars are found relative to the main jar it will work.
This issue was one of my main motivators to start using maven.
#YoK nicely quotes here the relevant reasons to this question https://stackoverflow.com/a/3589930/1493954
Here is one the relevant quotes from Benefits of using Maven
Henning
quick project setup, no complicated build.xml files, just a POM and go
all developers in a project use the same jar dependencies due to
centralized POM.
getting a number of reports and metrics for a project
"for free"
reduce the size of source distributions, because jars can
be pulled from a central location
This is a common problem for developement environments.
I myself use the following solution in netbeans [Hope so that it works in your IDE too]:
Create an Ant variable [for e.g. JAR_LOCATION].
Each and every JAR that you refer, must have it's location relative to JAR_LOCATION.
Now, in every development environment that you're gonna use, you can set up the same ANT variable and it'll automatically pick up the JAR.
You can see the example in the given snapshot of netbeans..

Where to add log4j configuration file in our project?

I have implemented log4j in my web application project. Project is done using net beans,using tomcat 7.0.41. At first,I created log4j.property file and placed under web page->Web-INF->classes->log4j.properties in net beans and it asks me to locate the file in my project,so I manually located that file to implement log4j in my application. After that I changed the place of the log4j.properties file to myproject->build->web->WEB_INF->classes->log4j.properties in location of my project saved, now its working fine, it did not ask me to manually locate the property file, It takes automatically when my class files executed. Now my problem is that once I committed the project and again checkout the project on some day, property file does not appear and it again ask for property file. So where can I create the log4j property file in my project so that my team mates can utilize it when they checkout project in their system.
Normally you put log4j.properties to src/main/resources/ and it will be copied to the right place by the build process.
I never use net beans, but I think put log4j.properties under Classpath will work.
Not sure how Net Beans handels this, but i think that the "build" directory is where the "compiled" project is put to.
So i would not recommend to put any files there which should be versioned because mostly those directories are ignored for versioning ( see .gitignore files for example when using git).
Resources like property files should be within the sources and your IDE should copy them to the correct place when building the project.

IntelliJ: Adding a dependency doesn't work but adding to classpath under SDK (Project settings) works

can anyone help?
I have reading various blogs and it appears to include i18n properties and otherfiles i have to add them as module dependency, choose Jar .. Directories and then chosing "classes" category.
I have done this but i keep getting an error saying that it can't find an XML file. Specfically this
admin-sidebar.xml
I managed to fix this by adding the 2 directories under Projec Settings, SDK, and adding them to classpath.
but this is annoying that it doesn't work the way in the tutorials.. i must be doing something wrong.
I have to add 2 directories into the class path.
the first directory has about 8 .properties files, these are standard javabundles.
the next directory has the following content
admin-sidebar.xml METAINF (a directory) mime.types (a file inside the
METAINF directory)
Can anyone tell me what i am doing wrong, of course i can leave it the way i have done which seems to fix the issue, but it just feels like i am not doing it right
Any ideas or help?
Thanks in advance
EDIT
Here is the project zipped up.
https://docs.google.com/open?id=0ByGL4ug6mtrsdFFNNmtZNXMxV0k
Ok, this is what i am doing, add modules but it doens't work
how it looks when adding, i choose classes
and this is how it looks when added
This above doesn't work, but adding it directly too the jdk class path works, notice its the same 2 dirs.
The contents of the directory are (in order)
You either configure it as a library (classpath) entry added to the module dependencies or you set up the directory as Sources so that these files are automatically added to the classpath (and copied to output according to Settings | Compiler | Resource Patterns).
If this approach doesn't work, please share a sample project with either configuration showing the issue.
See also my reply to the related question: Add a properties file to IntelliJ's classpath.

Categories

Resources