Tuesday, March 28, 2017

git notes

Git tools on windows

  • Git for Windows (git bash as CLI)
  • SourceTree (GUI for git)

Start git bash in specific directory on windows

Modify git bash shortcut - under Target:
instead
D:\Tools\Git\git-bash.exe --cd-to-home
put
D:\Tools\Git\git-bash.exe --cd=D:\git

Git usage

Clone repository

$ git clone URL

Switch to tag/branch

$ git checkout tags/<tag_name>

List all tags

$ git tag -l

Remove local repository

delete directory :)

Revert changes

# Revert changes to modified files.
$ git reset --hard

# Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`)
$ git clean -fd

Git stash

-- Stash the changes in a dirty working directory away
--  saves your local modifications away and reverts the working directory to match the HEAD commit.
$ git stash apply -- restore stashed files - you need to call git stash drop to remove files from stash!

$ git stash pop -- move files from stash into branch

## https://stackoverflow.com/questions/11269256/how-to-name-and-retrieve-a-stash-by-name-in-git
$ git stash save "guacamole sauce WIP"
$ git stash apply stash^{/guacamo}
or git stash apply stash@{n}


How to see remote location

$ git remote show origin

Ref

Getting Started with Git @dzone
Tutorial 1: Using branch
Git from the inside out
A successful Git branching model By Vincent Driessen

Friday, March 24, 2017

Ant notes

Multiple environments - properties overrides

By defining property files first one will override the next one that is defined:

<property file="build.${user.name}.properties" description="Local customizations, overrides"/>
<property file="build.properties" />


This means if you have property named databaseUrl defined in both files, the one with your username will have more precedence.

To ignore overrides that are based on user.name one can use antcall task instead depends and override params.
Example is bellow. In it you can override properties someParameter_1 and someParameter_2, by using antcall.

<target name="dist">

<antcall target="build">
<param name="someParameter_1" value="newValue"/>
<param name="someParameter_2" value="newValue"/>
</antcall>

<delete dir="${dist.dir}" />
<mkdir dir="${dist.dir}" />

<tar destfile="${dist.dir}/myProduct-${version}.tar.gz" compression="gzip">
<tarfileset dir="${build.dir}" />
</tar>
</target>


Ref

Managing Multiple Build Environments
Built-in Ant Properties
Apache Ant Wiki
AntCall

Thursday, March 2, 2017

Spring profiles

Requirement

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments.

I'm working on 2 applications:

  1. with Spring Boot (1.3)
  2. legacy Spring XML (Spring version 3.2.)
Requirements are to have active profiles define in properties file. This is easy and by default for Spring Boot application.

Setting active profiles in Spring Boot application


Define spring.profiles.active=myProfile inside properties file


Displaying active profile when application is starting:

@Autowired
protected Environment env;

@PostConstruct
void after() {
String[] activeProfiles = env.getActiveProfiles();
logger.info("\n** activeProfiles: " + Arrays.toString(activeProfiles));
}


Setting active profiles in legacy Spring applications (Spring 3.2)


Defining profile for legacy Spring application was done in web.xml:

<context-param>
<param-name>spring.profiles.active</param-name>

<param-value>myProfile</param-value>
</context-param>


Read active profile inside JSP page:
String activeProfile = (String) pageContext.getServletContext().getInitParameter("spring.profiles.active");

To move this definition inside properties file, you need to implement org.springframework.context.ApplicationContextInitializer interface:

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment env = applicationContext.getEnvironment();
try {
env.getPropertySources().addFirst(new ResourcePropertySource("classpath:application.properties"));

String profile = env.getProperty("spring.profiles.active");
logger.info("env.getProperty('spring.profiles.active'): " + profile);

String[] activeProfiles = env.getActiveProfiles();
logger.info("env.getActiveProfiles: " + Arrays.toString(activeProfiles));

// logger.info("changing active profile to ...");
// env.setActiveProfiles("newProfile");
} catch (IOException e) {
logger.info("file not found...");
}
}


In web.xml register it as context-param:

<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>hr.samara.demo.config.SamaraInitializer</param-value>
</context-param>


After this change reading active profile in JSP page needs to change:

<%@page import="org.springframework.web.context.support.XmlWebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>

<%
ServletContext sc = pageContext.getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
String[] profiles = applicationContext.getEnvironment().getActiveProfiles();
%>

Ref:

http://www.baeldung.com/spring-profiles
https://www.mkyong.com/spring/spring-profiles-example/
spring-doc-Bean definition profiles
stackoverflow - How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property